vijaycreations
Published in

vijaycreations

Continuously check for internet connectivity in Flutter apps.

In this article we will discuss about how to continuously check whether the device is connected to internet or not using flutter.

🎥 Video Tutorial

🛠️ Dependencies

Add the below two packages in pubspec.yaml file. 👇

🔭 Implementation

Summary
In this example we will have an elevated button at the center which when clicked will take as to the seconds page. This functionality will work only when the device is connected to the network. If the network connection is lost then, we will get an alert dialog as shown below.👇 Now even if you click this ‘ok’ button in the alert dialog, you will still see this alert box popping up again and again unless until the device is connected to a stable network.

  1. Building up the UI screens.

2. Implementing the logic.

Create StreamSubscription and listen to changes in network connectivity using onConnectivityChanged method available in connectivity_plus package. Therefore whenever there is change in network connection, we will be notified of the change.

We have completed listening to changes in the network, now we need to check for internet connection every time whenever the above said onConnectivityChanged method is triggered.

To do so., we will be making use of InternetConnectionChecker().hasConnection available in internet_connection_checker package. This will return a boolean value. (true-the device has internet connection, false-the device lacks internet connection).

Therefore based on above boolean value we will be displaying the alertbox.

Now with this we will be able to show alert-box as soon as the device connection is terminated, but in-order to check for connection continuously and show this alert dialog until the device is connected to the network, we need to perform the same conditional check inside the on press event of the ‘ok’ button present in that alert dialog.

Well that’s it. 🎉 Run the app to see it in action.🥳

This is how we can continuously check for internet connectivity in flutter apps.

Get the complete source code here : 👉 https://github.com/vijayinyoutube/connectivity_checker

Check out all my Flutter related blogs here.,👇

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vijay R

Vijay R

167 Followers

Hai👋 I’m a flutter developer experienced in designing and developing stunning mobile apps. Reach out for freelance projects: vijaycreations02@gmail.com