forward method

Future<void> forward(
  1. StreamController<T> controller,
  2. {bool cancelOnError = false,
  3. bool closeWhenDone = true}

Forwards all events and errors to controller, handling cases where controller is independently closed before this finishes.

If closeWhenDone is true, controller closes when this does. If cancelOnError is true, any errors emitted by this will trigger a cancellation of the subscription and act as though this finished.

Returns a Future which completes when one of the Streams finishes.

This differs from Stream.pipe and StreamConsumer.addStream in that controller can operate independent of this method's operations. Other events can be added to controller while this is being listened to, and if controller closes before this, no further events are added to controller.


Future<void> forward(
  StreamController<T> controller, {
  bool cancelOnError = false,
  bool closeWhenDone = true,
}) {
  final completer = Completer<void>.sync();
    (event) {
      if (!controller.isClosed) {
    onError: (Object e, StackTrace st) {
      if (!controller.isClosed) {
        controller.addError(e, st);
    onDone: () {
      if (closeWhenDone) {
    cancelOnError: cancelOnError,
  return Future.any([controller.done, completer.future]);