How to check if the Internet is connected or not in Flutter | Flutter Connectivity

Hello Guys, In this blog, we will learn How to check if the Internet is connected or not in flutter application. For this, we will use the Flutter Connectivity Library. Sometimes the internet is not available on the device and when building the application it throws an exception. When we integrate API or other methods that depend on the internet will not work. In this case, we need to handle these exceptions.

Introduction of Connectivity Library

This plugin is very useful for checking the internet connectivity in devices. Mobile always can access data using Mobile Cellular Connection and Wi-Fi mode. This plugin only works for Android and iOS. For the web it's not working yet, maybe in the future, it will work as flutter is getting updated day by day.

Add Connectivity Library in Pubspec.ymal file

The latest dependency version is : connectivity: ^0.4.9+3, you can get the latest version of dependency here (Click Here)

connectivity: ^0.4.9+3

After adding the dependency in the package's pubspec.yaml file, import the library where you want to use it.

import 'package:connectivity/connectivity.dart';

How to Detect Internet Connected or Not

Check if device is connected with internet or not. Need to write dart code like:

Step 1: Add Dependencies in pubspec.yaml file.

Dependencies details given above or you can check on website.

Step 2 : Import the Class package

Then, after adding the dependency, now you need to Import the package in dart files wherever we need. import 'package:connectivity/connectivity.dart';

Step 3: Create a method to detect internet connection.

var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
// Mobile is not Connected to Internet
else if (connectivityResult == {
// I am connected to a mobile network.

else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.

This code we can use with method or also can use in initState

Step 4: Get Details Connected Connection.

If you are connected with mobile or wifi you will get all details of network.

Get Wifi details:-

var wifiBSSID = await (Connectivity().getWifiBSSID());
var wifiIP = await (Connectivity().getWifiIP());
var wifiName = await (Connectivity().getWifiName());


Hope this article is useful for you. If you got any issue comment in the section below.

Thank You!

Next Post »