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
Add the below two packages in
pubspec.yaml file. 👇
internet_connection_checker | Dart Package
A Pure Dart Utility library that checks for an Active Internet connection by opening a socket to a list of specified…
connectivity_plus | Flutter Package
This plugin allows Flutter apps to discover network connectivity and configure themselves accordingly. It can…
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.
- Building up the UI screens.
2. Implementing the logic.
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.,👇
Other articles you may like.,
Shared Preferences in Flutter
Store the data locally in Flutter app using Shared Preferences.
Setting up your Flutter app for publishing in Play Store.
Before publishing our Flutter app in Google Play Store we need to brush up few feature to make sure that the app is…
Creating Desktop Apps using Flutter
In this article we will discuss about how to create and run a desktop app using Flutter.
If you found this article useful and wish to support my work, you can consider buying me a coffee.👇
VIJAY R is Content Creator
Hey 👋., I just started the page. Support by buying me a coffee.
If you want to know more about Flutter and various Widgets in Flutter…?🤓 Then visit my channel 👉🏻 vijaycreations🚩