addStream method
Receives events from source
and puts them into this controller's stream.
Returns a future which completes when the source stream is done.
Events must not be added directly to this controller using add, addError, close or addStream, until the returned future is complete.
Data and error events are forwarded to this controller's stream. A done
event on the source will end the addStream
operation and complete the
returned future.
If cancelOnError
is true
, only the first error on source
is
forwarded to the controller's stream, and the addStream
ends
after this. If cancelOnError
is false, all errors are forwarded
and only a done event will end the addStream
.
If cancelOnError
is omitted or null
, it defaults to false
.
Implementation
@override
Future<void> addStream(Stream<T> source, {bool? cancelOnError}) {
if (_isAddingStreamItems) {
throw StateError(
'You cannot add items while items are being added from addStream');
}
final completer = Completer<void>();
var isOnDoneCalled = false;
final complete = () {
if (!isOnDoneCalled) {
isOnDoneCalled = true;
_isAddingStreamItems = false;
completer.complete();
}
};
_isAddingStreamItems = true;
source.listen((T event) {
_add(event);
}, onError: (Object e, StackTrace? s) {
_addError(e, s);
if (identical(cancelOnError, true)) {
complete();
}
}, onDone: () {
complete();
}, cancelOnError: cancelOnError);
return completer.future;
}