Skip to content

Instantly share code, notes, and snippets.

@pradeeprjth
Created August 3, 2021 09:29
Show Gist options
  • Save pradeeprjth/e6610791c662d45a03eb58c656068398 to your computer and use it in GitHub Desktop.
Save pradeeprjth/e6610791c662d45a03eb58c656068398 to your computer and use it in GitHub Desktop.
// before rendering your main page use write a condition
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// this will show when your device is connected with mobile network
print("connected to mobile data");
// ignore: unused_local_variable
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Connected to mobile network'),
backgroundColor: Colors.green,
),
);
else if(connectivityResult == ConnectivityResult.wifi){
// this will show when your device is connected with wifi
print("connected to wifi");
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Connected to wifi network '),
backgroundColor: Colors.green,
),
);
}else {
// This will shown when your device is disconnected
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('No internet connection'),
backgroundColor: Colors.black,
action: SnackBarAction(
label: 'retry',
onPressed: () {
// Code to execute. or you can simply refresh your page you can also include an image
setState(() {
});
},
),
),
);
print("No internet connection");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment