Home > Software design >  Convert Stream<List<T>> to Stream<T>
Convert Stream<List<T>> to Stream<T>

Time:03-16

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]);

CodePudding user response:

You can use expand:

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

CodePudding user response:

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.

  •  Tags:  
  • dart
  • Related