Hi Guys, Welcome to Proto Coders Point, In this Flutter Tutorial we gonna see how to check if Internet is Connected in flutter using Flutter Connectivity Library. And if the Internet is Connect then whether it is using Mobile data or is in Wifi Connection. how to check internet connection continuously in flutter.
To study more about Connectivity Library in Flutter then visit here
Introduction to Flutter Connectivity Library
This Plugin is very useful if your app need internet connection to run the application perfectly, This Library allows your flutter Application to Discover Network Connectivity. This Flutter Library will also check if your mobile is currently using cellular mobile data or is using WiFi Connection.
This Flutter Plugin Perfectly works for Both Android and iOS devices, So it is been rated with 100 points in Flutter Library Store.
Final Output screen of this project
VIDEO TUTORIAL ON FLUTTER CHECK INTERNET CONNECTION
Then, let’s Start with adding this library into our Flutter Project.
Step 1: Adding Dependencies
Open pubspec.yaml file and add the following dependencies
dependencies: connectivity: ^0.4.8+2 // add this line
After adding the dependencies just hit Pub Get, This will download all the required packages of Flutter Connectivity library into your flutter project.
Step 2 : Import the Class plackage
Then, After adding the dependencies just you need to do is import the class package wherever it’s required.
import 'package:connectivity/connectivity.dart';
Step 3 : Snippet code How to use function or method from connectivity library
How to detect internet connection in flutter.
if internet connected, then Weather it mobile Data or Wifi Connection.
var connectivityResult = await (Connectivity().checkConnectivity()); if (connectivityResult == ConnectivityResult.none) { // Mobile is not Connected to Internet } else if (connectivityResult == ConnectivityResult.mobile) { // I am connected to a mobile network. } else if (connectivityResult == ConnectivityResult.wifi) { // I am connected to a wifi network. }
If mobile connectivity is Wifi then, This is How to get Wifi details in flutter.
var wifiBSSID = await (Connectivity().getWifiBSSID()); var wifiIP = await (Connectivity().getWifiIP());network var wifiName = await (Connectivity().getWifiName());wifi network
You can Also keep checking OnConnectivityChanged Like this :
@override initState() { super.initState(); subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { // Got a new connectivity status! }) } // Be sure to cancel subscription after you are done @override dispose() { super.dispose(); subscription.cancel(); }
Complete Source Code – How to check if internet is connected in Flutter?
main.dart
Copy Paste Below Lines of Flutter Code in main.dart file
import 'package:flutter/material.dart'; import 'package:connectivity/connectivity.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: HomeConnect(), ); } } class HomeConnect extends StatefulWidget { @override _HomeConnectState createState() => _HomeConnectState(); } class _HomeConnectState extends State<HomeConnect> { var wifiBSSID; var wifiIP; var wifiName; bool iswificonnected = false; bool isInternetOn = true; @override void initState() { // TODO: implement initState super.initState(); GetConnect(); // calls getconnect method to check which type if connection it } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Flutter Connectivity..."), ), body: isInternetOn ? iswificonnected ? ShowWifi() : ShowMobile() : buildAlertDialog(), ); } AlertDialog buildAlertDialog() { return AlertDialog( title: Text( "You are not Connected to Internet", style: TextStyle(fontStyle: FontStyle.italic), ), ); } Center ShowWifi() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( " Your are connected to ${iswificonnected ? "WIFI" : "MOBILE DATA"}"), Text(iswificonnected ? "$wifiBSSID" : "Not Wifi"), Text("$wifiIP"), Text("$wifiName") ], ), ); } Center ShowMobile() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(" Your are Connected to MOBILE DATA"), ], ), ); } void GetConnect() async { var connectivityResult = await (Connectivity().checkConnectivity()); if (connectivityResult == ConnectivityResult.none) { setState(() { isInternetOn = false; }); } else if (connectivityResult == ConnectivityResult.mobile) { iswificonnected = false; } else if (connectivityResult == ConnectivityResult.wifi) { iswificonnected = true; setState(() async { wifiBSSID = await (Connectivity().getWifiBSSID()); wifiIP = await (Connectivity().getWifiIP()); wifiName = await (Connectivity().getWifiName()); }); } } }
Recommended Tech Articles
android check internet connection continuously
fetching data in flutter and displaying in listview – http example