onErrorReturnWith method Null safety

Stream<T> onErrorReturnWith(
  1. T returnFn(
    1. Object error,
    2. StackTrace stackTrace

Instructs a Stream to emit a particular item created by the returnFn when it encounters an error, and then terminate normally.

The onErrorReturnWith operator intercepts an onError notification from the source Stream. Instead of passing it through to any observers, it replaces it with a given item, and then terminates normally.

The returnFn receives the emitted error and returns a value. You can perform logic in the returnFn to return different value based on the type of error that was emitted.

If you do not need to perform logic based on the type of error that was emitted, please consider using onErrorReturn.


  .onErrorReturnWith((e, st) => e is Exception ? 1 : 0)
  .listen(print); // prints 1


Stream<T> onErrorReturnWith(
        T Function(Object error, StackTrace stackTrace) returnFn) =>
        (e, st) => Stream.value(returnFn(e, st))).bind(this);