flutterdartrxdart

Transform Stream<Item> to Stream<List<Item>>


I try to get all files in folder by Stream.

Directory(path).list() returns Stream<FileSystemEntity>.

I want returns Stream<List<FileSystemEntity>>.

Example:

1 => 1
2 => 1,2
3 => 1,2,3

Solution

  • scan operator from rxdart is the best answer
    https://pub.dev/documentation/rxdart/latest/rx/ScanExtension/scan.html https://rxjs.dev/api/operators/scan

    Stream<FileSystemEntity> source$ = ...;
    
    Stream<List<FileSystemEntity>> result$ = source$.scan(
        (acc, element, _) => [...acc, element],
        [],
    );