Is your Flutter App Internet Sensitive?

Salman Shaikh
Canadiv’s Technology and Design
3 min readOct 27, 2022

--

Does your Flutter App connect with the internet services? Still in the development phase and want to check? Follow the steps as mentioned below in this Flutter (Dart) language-based article:

Flutter Application connected to the Internet?

It is essential for every mobile or web application to be connected with the internet. We will be using the following syntax to check our internet connectivity:

InternetAddress.lookup()

Moreover, we will be using Google.com, as it has the most famous online presence in our industry.

If we do connect with the internet, there will be a response from the network. Otherwise, this method will turn out to be a Socket Exception.

To further understand this, we will use try catch block with Socket Exception.

The following is the syntax for the code:

try { await InternetAddress.lookup(‘google.com’).then((value) {  if (value.isNotEmpty) {   //to do when network is there  
} else {
//to do when no network }
});} on SocketException catch (e) { //to do when no network} catch (e) { print(“$e”);}

The above syntax will help check internet connectivity with our Flutter application. Moreover, we will allot time to inspect the connectivity using:

Time.periodic(Duration(seconds:3),(timer){})

The duration of 3 seconds is an example. You can alter it based on your own understanding and experimentation. Merge the duration code as shown below:

Timer.periodic(const Duration(seconds: 3), (timer) async {try {await InternetAddress.lookup(‘google.com’).then((value) {if (value.isNotEmpty) {//to do when network is there} else {// to do when no network}});} on SocketException catch (e) {// to do when no network} catch (e) {print(“$e”);}});

How to check the internet availability using Riverpod?

This is an alternate method that works using Riverpod. If you want to check internet connectivity with your Flutter application, use the Changenotifier.

We have created an Enum for the Riverpod version as stated below:

enum ConnectivityStatus { online, offline }

Another version of it:

final homeProvider = ChangeNotifierProvider((ref) {final model = HomeNotifer();model.init();return model;});class HomeNotifer extends ChangeNotifier {ConnectivityStatus _network = ConnectivityStatus.offline;ConnectivityStatus get network => _network;set network(ConnectivityStatus network) {_network = network;notifyListeners();}void init() {getConnectivity();}void getConnectivity() async {Timer.periodic(const Duration(seconds: 3), (timer) async {try {await InternetAddress.lookup(‘google.com’).then((value) {if (value.isNotEmpty) {//to do when network is therenetwork = ConnectivityStatus.online;} else {// to do when no networknetwork = ConnectivityStatus.offline;}});} on SocketException catch (e) {// to do when no networknetwork = ConnectivityStatus.offline;} catch (e) {print(“getCon $e”);}});notifyListeners();}}

Connecting it with the homepage:

class MyHomePage extends ConsumerWidget {const MyHomePage({Key? key}) : super(key: key);@overrideWidget build(BuildContext context, WidgetRef ref) {final model = ref.watch(homeProvider);return Scaffold(body: Center(child: Text(model.network == ConnectivityStatus.online? “Online”: “Offline”),),);}}

Both the above methods help in checking the internet availability for your Flutter Application.

Follow and stay connected with Canadiv Publication to stay informed about valuable insights and the most current trends within the development ecosystem.
Follow Us:
LinkedIn | Instagram

--

--