flattenToList method
Gathers all lists emitted by this stream and flattens them into a single list.
cancelOnError: Iftrue(default), the stream subscription is cancelled immediately upon the first error. Iffalse, the stream continues to drain in the background, but the returned Future completes with the first error.sync: Iftrue, the internal Completer is created asCompleter.sync(), completing the Future synchronously upon the stream'sonDoneevent.
Implementation
Future<List<T>> flattenToList({
bool cancelOnError = true,
bool sync = false,
}) {
final completer = sync ? Completer<List<T>>.sync() : Completer<List<T>>();
final accumulated = <T>[];
listen(
(list) {
if (!completer.isCompleted) {
accumulated.addAll(list);
}
},
onError: (Object error, StackTrace stackTrace) {
if (!completer.isCompleted) {
completer.completeError(error, stackTrace);
}
},
onDone: () {
if (!completer.isCompleted) {
completer.complete(accumulated);
}
},
cancelOnError: cancelOnError,
);
return completer.future;
}