dart-lang / async

A Dart package that contains tools to work with asynchronous computations.
https://pub.dev/packages/async
BSD 3-Clause "New" or "Revised" License
320 stars 50 forks source link

`streamGroup.stream.toList()` never return #283

Open stephane-archer opened 2 months ago

stephane-archer commented 2 months ago
import 'dart:io';

import 'package:async/async.dart';

Future<void> main(List<String> arguments) async {
  List<Directory> ld = [Directory('lib')];
  var streamGroup = StreamGroup<FileSystemEntity>();
  for (var dir in ld) {
    var steam = dir.list();
    streamGroup.add(steam);
  }
  var fileList = await streamGroup.stream.toList();
  print(fileList);
}

await streamGroup.stream.toList(); never return

lrhn commented 1 month ago

Writing the class docs:

stream won't close until close is called on the group and every stream in the group closes

The toList waits for the stream to close, which it doesn't. Call streamGroup.ckose() after the loop, when you know you won't add more streams.