【问题标题】:Dart / Flutter: Error "Stream has already been listened to." && "await for" inside fa "for loop" failsDart / Flutter:错误“流已被收听。” fa“for循环”中的&&“等待”失败
【发布时间】:2019-08-27 03:47:34
【问题描述】:

好人啊,

我可能在这里遗漏了一些东西:

这段代码是虚构的(为了大家方便,它过于简单化了),但给出了这个想法:

_map.keys.forEach((key) async {
   _bloc.sink.add(_map[key]);
await for (String _string in _bloc.stream) {
  _newMap.putIfAbsent(key, ()=> _string);
 }
});

或

Stream.fromIterable(_map.keys).forEach((day) async {
    _bloc.sink.add(_map[key]);
await for (String _string in _bloc.stream) {
  _newMap.putIfAbsent(key, ()=> _string);
 }
});

与 StreamController<...>.broadcast(); 溪流接缝健康:

first:_Future hashCode:817453996 isBroadcast:true isEmpty:_Future 最后:_未来长度:_未来运行时类型:类型 (_BroadcastStream<...>) 单曲:_Future _awaiter:null _generator:null _controller:_AsyncBroadcastStreamController

不返回此错误:

first:Unhandled exception:\nBad state: Stream has already been 听过。[...] hashCode:644468606 isBroadcast:false isEmpty:Unhandled exception:\nBad state: Stream has already been 听过。[...] last:Unhandled exception:\nBad state: Stream has 已被收听。[...] 长度:未处理的异常:\n错误状态: Stream 已经被监听。[...] runtimeType:Type (_ControllerStream<...>) single:未处理的异常:\n错误状态: 流已被收听。[...] _awaiter:null _generator:null _controller:_AsyncStreamController

在这两种情况下都不起作用。

我打算做的是(在 for 循环内)向接收器发送一个集合,

一个块会做一些工作并在流中返回另一个集合,

我从调试中看到的是循环只是不断向接收器发送东西。

我不确定是因为:

...

1)循环不等待“等待”

并导致错误“流已侦听”

2)反之亦然“等待”因为错误而无法收听流

(或者因为广播可能会丢失事件)因此循环继续循环(?);

...

我更倾向于第一个,但它应该根据这个POST工作

我做错了什么?

提前谢谢你,弗朗西斯科

【问题讨论】:

    标签: dart flutter stream iteration


    【解决方案1】:

    您似乎不止一次调用了 stream.listen。尝试将 _bloc.stream 分配给 for 循环之外的变量。

    var stream = _bloc.stream;
    
    
    _map.keys.forEach((key) async {
      _bloc.sink.add(_map[key]);
    await for (String _string in stream) {
     _newMap.putIfAbsent(key, ()=> _string);
    }
    });
    

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2021-12-26
      • 1970-01-01
      • 2023-02-16
      • 2023-03-29
      • 2020-02-21
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      相关资源
      最近更新 更多