'Dart `asyncExpand` not working as expected
I am trying to create the following stream baz which consists of an "outer" stream, where each of this stream's events also has an "inner" stream.
For each event X in the outer stream, all the events in the inner stream for X should be added to baz.
class Foo {
final StreamController<int> bar = StreamController();
Foo() {
getsAnOuterStream.listen((event) {
bar.addStream(getsAnInnerStream(event));
});
}
Stream<int> get baz => bar.stream;
}
The above code works as intended. However, I had understood that I could simply achieve this by using the asyncExpand method instead.
class Foo {
final Stream<int> baz = getsAnOuterStream
.asyncExpand((event) => getsAnInnerStream(event));
}
This does not work – when the outer stream changes, the new inner stream events are not added to baz. Is there a subtlety that I'm missing here? Any help is much appreciated!
Just to note... I think the problem could be to do with this: if an inner stream goes on forever, baz will never move on to the events from the next inner stream. However, if this is the issue, why does the top solution work?
Solution 1:[1]
Thanks hitecherik for the great explanation and I found a solution, please use switchMap from stream_transform.
Flatten a Stream of Streams into a Stream which forwards values from the most recent Stream
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chan Chun Him |
