'Convert Stream<List<T>> to Stream<T>

I try to convert Stream of List to a single Stream of T.

I want to achieve Stream<T> from Sream<List<T>> in pure dart.

Example:

Convert stream that emits these values

Stream.fromIterable([[1, 2, 3], [4, 5, 6]]);

To stream that emits these values

Stream.fromIterable([1, 2, 3, 4, 5, 6]);


Solution 1:[1]

You can use expand:

stream = streamOfList.expand((e) => e);

Solution 2:[2]

You can use a function like:

Stream<T> flatten<T>(Stream<List<T>> source) async* {
  await for (var list in source) {
    for (var element in list) {
      yield element;
    }
  }
}

Or use expand as suggested.

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 Alexandre Ardhuin
Solution 2 lrn