title: Check or listen continuously to internet connection/Network Connectivity in dart, flutter app
body: <p>I have been searching for long to know the best approach to listen to internet connection in flutter/dart app. I think this approach is better for now and it can be of help to some like me who has been searching. I have used many connectivity plugins, but it didn't work. I have equally used data_connection_checker, lookUpAddress etc as suggested by many but to no avail. But below helped.
  Use the below plugins to check or listen to Internet Connection / Network Connectivity in dart, flutter app.</p>
  <p><a href="https://pub.dev/packages/connectivity_plus" rel="noreferrer">connectivity_plus</a></p>
  <p><a href="https://pub.dev/packages/internet_connection_checker" rel="noreferrer">internet_connection_checker</a></p>
  <pre><code>import 'dart:async';
  import 'package:connectivity_plus/connectivity_plus.dart';
  import 'package:internet_connection_checker/internet_connection_checker.dart';
  
  class ConnectionUtil {
    static final ConnectionUtil _singleton = new ConnectionUtil._internal();
    ConnectionUtil._internal();
  
    static ConnectionUtil getInstance() =&gt; _singleton;
  
    bool hasConnection = false;
  
    StreamController connectionChangeController = StreamController();
  
    final Connectivity _connectivity = Connectivity();
    void initialize() {
      _connectivity.onConnectivityChanged.listen(_connectionChange);
    }
  
    void _connectionChange(ConnectivityResult result) {
      _hasInternetInternetConnection();
    }
  
    Stream get connectionChange =&gt; connectionChangeController.stream;
    Future&lt;bool&gt; _hasInternetInternetConnection() async {
      bool previousConnection = hasConnection;
      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.mobile || connectivityResult == ConnectivityResult.wifi) {
        // this is the different
        if (await InternetConnectionChecker().hasConnection) {
          hasConnection = true;
        } else {
          hasConnection = false;
        }
      } else {
        hasConnection = false;
      }
  
      if (previousConnection != hasConnection) {
        connectionChangeController.add(hasConnection);
      }
      return hasConnection;
    }
  }
  
  </code></pre>
  <p>Implement this code on the stateful widget.....</p>
  <pre><code>  bool hasInterNetConnection = false;
  
    @override
    initState() {
      ConnectionUtil connectionStatus = ConnectionUtil.getInstance();
      connectionStatus.initialize();
      connectionStatus.connectionChange.listen(connectionChanged);
  
      super.initState();
    }
  
    void connectionChanged(dynamic hasConnection) {
      setState(() {
        hasInterNetConnection = hasConnection;
      });
    }
  
  </code></pre>
  <p>Good luck</p>