Created
December 27, 2018 13:10
-
-
Save MDSADABWASIM/b4940c4ada4feef4b4e8db9f234e527b to your computer and use it in GitHub Desktop.
manage connectivity in flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart.io'; //use this | |
bool connected=false; // and define this global variable. | |
Future<bool> _checkConnectivity() async { | |
bool connect; | |
try { | |
final result = await InternetAddress.lookup('google.com'); | |
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { | |
connect = true; | |
} | |
} on SocketException catch (_) { | |
connect = false; | |
} | |
return connect; | |
} | |
@override | |
Widget build(BuildContext context) { | |
height = MediaQuery.of(context).size.height; | |
width = MediaQuery.of(context).size.width; | |
_checkConnectivity().then((internet) { | |
setState(() { | |
connected = internet; | |
}); | |
}); | |
return new Scaffold( | |
//use that boolean here. | |
body:connected? Listview() : noInternetImage(): | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment