简体   繁体   English

E/EventChannel#plugins.flutter.io/connectivity_status(28677):无法关闭现有的事件流

[英]E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream

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 connectivity 0.4.9+5 and data_connection_checker 0.3.4 in combination to check if user has active connection or not.我结合使用连接 0.4.9+5data_connection_checker 0.3.4来检查用户是否有活动连接。

I have create one file internet_connectivity_service.dart which perform all the task related to it.我创建了一个文件 internet_connectivity_service.dart 来执行与之相关的所有任务。

class InternetConnectivityService {
  StreamSubscription<ConnectivityResult> _subscription;

  /*
    * This function check if the device has internet connectivity.
    * With the help of DataConnectionChecker it checks for actual internet connectivity. 
  */
  Future<bool> _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<bool> which perform async request to check connectivity.
    * After checking connection type it check for divice has internet accesssible or not.
  */
  Future<bool> _hasInternet() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      return _checkConnectivity();
    } else if (connectivityResult == ConnectivityResult.wifi) {
      return _checkConnectivity();
    } else {
      return false;
    }
  }

  StreamSubscription<ConnectivityResult> 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();
  }
}

Function initInterNetConnectivitySubscription and disposeSubscription are used for subscribe and unsubscribe for particular event.函数initInterNetConnectivitySubscriptiondisposeSubscription用于订阅和取消订阅特定事件。 I am calling these methods from component where I want this functionality.我正在从需要此功能的组件中调用这些方法。

For Example:例如:

     InternetConnectivityService internetConnectivityService =
      InternetConnectivityService();

  @override
  initState() {
    internetConnectivityService.initInterNetConnectivitySubscription(context);
    super.initState();
  }

  @override
  dispose() {
    internetConnectivityService.disposeSubscription();
    super.dispose();
  }

By doing so functionality is working sometimes but sometimes I am facing error stating E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream .通过这样做,功能有时会起作用,但有时我会E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream错误说明E/EventChannel#plugins.flutter.io/connectivity_status(28677): Failed to close existing event stream

Complete stack trace.完整的堆栈跟踪。

   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**

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.我只是不想拥有这样的功能:当用户没有连接时,应用程序会显示您没有连接到互联网。

I assume you have already solved this problem so this if for the people who come in the future and still have this problem.我假设你已经解决了这个问题,所以如果将来来的人仍然有这个问题。

I fixed it by upgrading to the latest version where they fixed the issue:我通过升级到他们解决问题的最新版本来修复它:

connectivity: ^2.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误E / BitmapFactory:无法解码流:java.io.FileNotFoundException ::打开失败:ENOENT(无此类文件或目录) - Error E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException:: open failed: ENOENT (No such file or directory) Java BufferedReader IO错误。 流关闭 - Java BufferedReader IO Error. Stream Close Flutter.io Android 许可证状态未知 - Flutter.io Android License Status Unknown Spring Cloud Stream Kafka的Azure Event Hub连接问题 - Azure Event Hub connectivity issues with Spring Cloud Stream Kafka 如何关闭java sdk中的流事件? - how to close a stream event in java sdk? 通过EventChannel向Flutter发送Android SMS BroadcastReceiver数据 - Sending Android SMS BroadcastReceiver data to Flutter through EventChannel java.io.StreamCorruptedException:无效的流头:7371007E - java.io.StreamCorruptedException: invalid stream header: 7371007E java.io.IOException:setdatasource失败状态= 0xFFF - java.io.IOException:setdatasource failed status = 0xFFF 错误:连接关闭:读取失败。 可能遇到流结束 - Error: Connection close: Read failed. Possible end of stream encountered java.io.EOFException:没有更多可用数据 - 预期的结束标记 </stream:stream> 关闭开始标记 <stream:stream> - java.io.EOFException: no more data available - expected end tag </stream:stream> to close start tag <stream:stream>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM