How to Check Whether there is an Internet connection available on Flutter app?
While Using a Flutter Mobile Application you may have seen that some mobile application doesn’t have access to some functionality without the Internet. In that case, users require to Check Whether there is an Internet connection available or not? So we will go through the same within this article.
How to Check Whether there is an Internet Connection available on the Flutter App?
The connectivity plugin states in its docs that it only provides information if there is a network connection, but not if the network is connected to the Internet.
Add Dependency to pubspec.yaml like below:
dependencies: connectivity: ^0.4.2
Then users need to import the package below:
import 'dart:io';
try { final result = await InternetAddress.lookup('google.com'); if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { print('connected'); } } on SocketException catch (_) { print('not connected'); }
Users can also try the below method:
import 'package:connectivity/connectivity.dart';
Future<bool> check() async { var connectivityResult = await (Connectivity().checkConnectivity()); if (connectivityResult == ConnectivityResult.mobile) { return true; } else if (connectivityResult == ConnectivityResult.wifi) { return true; } return false; }
Conclusion:
So in this article, We have been through How to check whether there is an Internet Connection available on the Flutter app.
Thanks for Reading!!!
Keep Fluttering.
Do let us know if you need any assistance with Flutter? we’d love to assist you:)
FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.
FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.
Tim Kariuki
Correct me if I am wrong… The connectivity plugin only determines if you are connected to the network via wifi or mobile. You may be connected to either with no internet connection. Right?
Somyarajsinh jadeja
Yes right!!
you can refer to https://pub.dev/packages/data_connection_checker 🙂
If you are still not able to get your and please let us know !!