Skip to content

Instantly share code, notes, and snippets.

@MDSADABWASIM
Created December 27, 2018 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MDSADABWASIM/b4940c4ada4feef4b4e8db9f234e527b to your computer and use it in GitHub Desktop.
Save MDSADABWASIM/b4940c4ada4feef4b4e8db9f234e527b to your computer and use it in GitHub Desktop.
manage connectivity in flutter
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