How to Check Internet Connectivity in a Flutter App

How to Check Internet Connectivity in a Flutter App

19 December 2024 Stephan Petzl Leave a comment Tech-Help

In mobile app development, ensuring that your application can reliably determine internet connectivity is crucial. For a Flutter app, this can be a bit tricky as you need to not only check for network availability but also confirm that the network has internet access. This guide will help you implement a robust solution to check internet connectivity in your Flutter applications.

Understanding the Problem

Many developers initially use the connectivity package to check for internet connectivity. However, this package only verifies if the device is connected to a network, such as Wi-Fi or mobile data, without guaranteeing actual internet access. This can be problematic, especially on networks that require additional authentication, such as hotel Wi-Fi.

To properly check for internet connectivity, you can implement a solution that combines network checks with actual internet access verification. Below is a modern approach using the connectivity_plus package, which is currently recommended over the deprecated connectivity package.

Step-by-Step Implementation

  1. Add Dependencies: Include the necessary packages in your pubspec.yaml file:

    dependencies:
      connectivity_plus: ^2.0.2
      internet_connection_checker: ^0.0.1+2
  2. Check Internet Connection: Implement a function to verify internet connectivity:

    import 'dart:async';
    import 'package:connectivity_plus/connectivity_plus.dart';
    import 'package:internet_connection_checker/internet_connection_checker.dart';
    
    class InternetCheck {
      Future isConnected() async {
        var connectivityResult = await Connectivity().checkConnectivity();
        if (connectivityResult == ConnectivityResult.mobile || 
            connectivityResult == ConnectivityResult.wifi) {
          return await InternetConnectionChecker().hasConnection;
        }
        return false;
      }
    }
  3. Use the Function: Call this function in your app to check for internet connectivity:

    void main() async {
      bool isOnline = await InternetCheck().isConnected();
      print(isOnline ? 'Connected to the Internet' : 'No Internet connection');
    }

Integrating with Your App

By implementing the above solution, you can ensure that your app not only detects network availability but also confirms actual internet access. This approach helps prevent issues related to false-positive connectivity checks.

Enhancing Test Automation with Repeato

If you are developing mobile applications using Flutter, consider using Repeato for your test automation needs. Repeato is a no-code test automation tool designed for iOS and Android apps. It leverages computer vision and AI to help you create, run, and maintain automated tests efficiently. With Repeato, you can quickly edit and execute tests, ensuring that your app’s connectivity features work reliably across different scenarios.

Like this article? there’s more where that came from!