addStream method

  1. @override
Future<void> addStream(
  1. Stream<T> source, {
  2. bool? cancelOnError,
})
override

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');
  }
  _isAddingStreamItems = true;

  final completer = Completer<void>();
  void complete() {
    if (!completer.isCompleted) {
      _isAddingStreamItems = false;
      completer.complete();
    }
  }

  source.listen(
    _add,
    onError: identical(cancelOnError, true)
        ? (Object e, StackTrace s) {
            _addError(e, s);
            complete();
          }
        : _addError,
    onDone: complete,
    cancelOnError: cancelOnError,
  );

  return completer.future;
}