πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

Flutter Check whether there is an Internet connection available or not

Flutter

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Flutter Check whether there is an Internet connection available or not

first install this package connectivity

connectivity

Run this command:

With Flutter:

 $ flutter pub add connectivity

Import it

Now in your Dart code, you can use:

import 'package:connectivity/connectivity.dart';

Main.dart

// 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");
}