title: E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream
body: <p>I have started development in flutter. In one scenario I have to check if the user is connected to internet or not. I am using <a href="https://pub.dev/packages/connectivity" rel="noreferrer">connectivity 0.4.9+5</a> and <a href="https://pub.dev/packages/data_connection_checker" rel="noreferrer">data_connection_checker 0.3.4</a> in combination to check if user has active connection or not.</p>
  <p>I have create one file internet_connectivity_service.dart which perform all the task related to it.</p>
  <pre><code>class InternetConnectivityService {
    StreamSubscription&lt;ConnectivityResult&gt; _subscription;
  
    /*
      * This function check if the device has internet connectivity.
      * With the help of DataConnectionChecker it checks for actual internet connectivity. 
    */
    Future&lt;bool&gt; _checkConnectivity() async {
      if (await DataConnectionChecker().hasConnection) {
        return true;
      } else {
        return false;
      }
    }
  
    /*
      * This function is invoked to on each connection change request.
      * Using Connectivity package it will check for connectivity and connection type.
      * This function has return type of Future&lt;bool&gt; which perform async request to check connectivity.
      * After checking connection type it check for divice has internet accesssible or not.
    */
    Future&lt;bool&gt; _hasInternet() async {
      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.mobile) {
        return _checkConnectivity();
      } else if (connectivityResult == ConnectivityResult.wifi) {
        return _checkConnectivity();
      } else {
        return false;
      }
    }
  
    StreamSubscription&lt;ConnectivityResult&gt; initInterNetConnectivitySubscription(
        BuildContext context) {
      // * Subscribe to connectivity changes
      _subscription = Connectivity().onConnectivityChanged.listen(
        (ConnectivityResult result) {
          _hasInternet().then(
            (result) {
              // * When internet is disconnected
              Size size = MediaQuery.of(context).size;
              if (!result) {
                Scaffold.of(context).showSnackBar(
                  snackBarBuilder(
                      color: kErrorColor,
                      text: 'Connection Lost',
                      context: context),
                );
              } else {
                Scaffold.of(context).showSnackBar(
                  snackBarBuilder(
                      color: kSuccessColor, text: 'Connected', context: context),
                );
              }
            },
          );
        },
      );
      return _subscription;
    }
  
    disposeSubscription() {
      _subscription.cancel();
    }
  }
  
  </code></pre>
  <p>Function <code>initInterNetConnectivitySubscription</code> and <code>disposeSubscription</code> are used for subscribe and unsubscribe for particular event. I am calling these methods from component where I want this functionality.</p>
  <p>For Example:</p>
  <pre><code>     InternetConnectivityService internetConnectivityService =
        InternetConnectivityService();
  
    @override
    initState() {
      internetConnectivityService.initInterNetConnectivitySubscription(context);
      super.initState();
    }
  
    @override
    dispose() {
      internetConnectivityService.disposeSubscription();
      super.dispose();
    }
  
  </code></pre>
  <p>By doing so functionality is working sometimes but sometimes I am facing error stating  <code>E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream</code>.</p>
  <p>Complete stack trace.</p>
  <pre><code>   Restarted application in 5,259ms.
  E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream
  E/EventChannel#plugins.flutter.io/connectivity_status(28677): java.lang.IllegalArgumentException: NetworkCallback was not registered
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at com.android.internal.util.Preconditions.checkArgument(Preconditions.java:50)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at android.net.ConnectivityManager.unregisterNetworkCallback(ConnectivityManager.java:4009)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at io.flutter.plugins.connectivity.ConnectivityBroadcastReceiver.onCancel(ConnectivityBroadcastReceiver.java:53)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onListen(EventChannel.java:182)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler.onMessage(EventChannel.java:167)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at android.os.MessageQueue.nativePollOnce(Native Method)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at android.os.MessageQueue.next(MessageQueue.java:336)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at android.os.Looper.loop(Looper.java:174)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at android.app.ActivityThread.main(ActivityThread.java:7356)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at java.lang.reflect.Method.invoke(Native Method)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  E/EventChannel#plugins.flutter.io/connectivity_status(28677):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)**strong text**
  
  </code></pre>
  <p>Due to this error my application is not getting crash but sometime functionality doesnot work as desired. Please let me know if I am making some mistake or if their is better way of doing so. I just wan't to have functionality in which when user don't have connection than application will show stating that you are not connected to internet.</p>