Reputation: 2657
aye aye good people,
I'm probably missing something here:
this code is fictional (it's an oversimplification, for everyone convenience), but gives the idea:
_map.keys.forEach((key) async {
_bloc.sink.add(_map[key]);
await for (String _string in _bloc.stream) {
_newMap.putIfAbsent(key, ()=> _string);
}
});
or
Stream.fromIterable(_map.keys).forEach((day) async {
_bloc.sink.add(_map[key]);
await for (String _string in _bloc.stream) {
_newMap.putIfAbsent(key, ()=> _string);
}
});
with StreamController<...>.broadcast(); the streams seams healthly:
first:_Future hashCode:817453996 isBroadcast:true isEmpty:_Future last:_Future length:_Future runtimeType:Type (_BroadcastStream<...>) single:_Future _awaiter:null _generator:null _controller:_AsyncBroadcastStreamController
without returns this error:
first:Unhandled exception:\nBad state: Stream has already been listened to.[...] hashCode:644468606 isBroadcast:false isEmpty:Unhandled exception:\nBad state: Stream has already been listened to.[...] last:Unhandled exception:\nBad state: Stream has already been listened to.[...] length:Unhandled exception:\nBad state: Stream has already been listened to.[...] runtimeType:Type (_ControllerStream<...>) single:Unhandled exception:\nBad state: Stream has already been listened to.[...] _awaiter:null _generator:null _controller:_AsyncStreamController
in both cases, doesn't work.
what I intended to do is (inside a for loop) to send in the sink a collection,
a bloc will do some work and return another collection in the stream,
what I can see from debug is that the loop just keep sending stuff to the sink.
I'm not sure that is because:
...
1) the loop doesn't wait for "await for"
and causes the error "stream already listen"
2) vice-versa the "await for " cannot listen to the stream because of the error
(or because the broadcast can lose the event) therefore the loop keep looping(?);
...
I'm more incline to about the first one, but it should work according to this POST
what am I doing wrong?
thank you in advance, Francesco
Upvotes: 0
Views: 1019
Reputation: 344
This seems like you calling stream.listen more than once. Try to assign the _bloc.stream to a variable outside of your for loop.
var stream = _bloc.stream;
_map.keys.forEach((key) async {
_bloc.sink.add(_map[key]);
await for (String _string in stream) {
_newMap.putIfAbsent(key, ()=> _string);
}
});
Upvotes: 1