tap method

Stream<T> tap(
  1. void onValue(
    1. T
    )?, {
  2. void onError(
    1. Object,
    2. StackTrace
    )?,
  3. void onDone()?,
})

Taps into this stream to allow additional handling on a single-subscriber stream without first wrapping as a broadcast stream.

The onValue callback will be called with every value from this stream before it is forwarded to listeners on the resulting stream. May be null if only onError or onDone callbacks are needed.

The onError callback will be called with every error from this stream before it is forwarded to listeners on the resulting stream.

The onDone callback will be called after this stream closes and before the resulting stream is closed.

Errors from any of the callbacks are caught and ignored.

The callbacks may not be called until the tapped stream has a listener, and may not be called after the listener has canceled the subscription.

Implementation

Stream<T> tap(void Function(T)? onValue,
        {void Function(Object, StackTrace)? onError,
        void Function()? onDone}) =>
    transformByHandlers(onData: (value, sink) {
      try {
        onValue?.call(value);
      } catch (_) {/*Ignore*/}
      sink.add(value);
    }, onError: (error, stackTrace, sink) {
      try {
        onError?.call(error, stackTrace);
      } catch (_) {/*Ignore*/}
      sink.addError(error, stackTrace);
    }, onDone: (sink) {
      try {
        onDone?.call();
      } catch (_) {/*Ignore*/}
      sink.close();
    });