Flutter不良状态:流已经听过
我试图在扑朔迷离中使用MethodChannel从本机Android代码接收事件。 即使我设置了AsbroadcastStream()
接收者
static Stream<dynamic> get accessStream {
_stream ??= _eventChannel.receiveBroadcastStream().map<dynamic>(
(event) {
return MainEvent.fromMap(event);
},
);
return _stream!.asBroadcastStream();
}
。
subs = Receiver.accessStream.listen((event) async {
if (event != null) {
events.add(event);
}
});
//After Some Time in Cancel Function
await subs.cancel()
但是,一旦我取消流订阅, 第一次,但是当我在app_listen.dart中调用相同功能时。它给出以下错误,
[ +100 ms] E/flutter (32159): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Bad state: Stream has already been listened to.
[ ] E/flutter (32159): #0 _StreamController._subscribe (dart:async/stream_controller.dart:676:7)
[ ] E/flutter (32159): #1 _ControllerStream._createSubscription (dart:async/stream_controller.dart:827:19)
[ ] E/flutter (32159): #2 _StreamImpl.listen (dart:async/stream_impl.dart:473:9)
[ ] E/flutter (32159): #3 listenEvent(package:trying/util/struct/app_listen.dart:49:51)
[ ] E/flutter (32159): <asynchronous suspension>
感谢您的回答。
I am trying to use methodChannel in flutter to receive events from native Android code. But once I cancel the stream subscription I can no longer create new subscriptons even if I set the stream asBroadcastStream()
Receiver.dart
static Stream<dynamic> get accessStream {
_stream ??= _eventChannel.receiveBroadcastStream().map<dynamic>(
(event) {
return MainEvent.fromMap(event);
},
);
return _stream!.asBroadcastStream();
}
When trying to call the function
subs = Receiver.accessStream.listen((event) async {
if (event != null) {
events.add(event);
}
});
//After Some Time in Cancel Function
await subs.cancel()
It works for the first time but when I call the same function in app_listen.dart again. It gives the following error
[ +100 ms] E/flutter (32159): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Bad state: Stream has already been listened to.
[ ] E/flutter (32159): #0 _StreamController._subscribe (dart:async/stream_controller.dart:676:7)
[ ] E/flutter (32159): #1 _ControllerStream._createSubscription (dart:async/stream_controller.dart:827:19)
[ ] E/flutter (32159): #2 _StreamImpl.listen (dart:async/stream_impl.dart:473:9)
[ ] E/flutter (32159): #3 listenEvent(package:trying/util/struct/app_listen.dart:49:51)
[ ] E/flutter (32159): <asynchronous suspension>
Thanks for answering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过删除
?? =
并用=
在receiver.dart中替换它来解决问题。I have solved the issue by just removing the
??=
and replacing it with=
in Receiver.dart