onErrorReturnWith method

Observable<T> onErrorReturnWith(
  1. T returnFn(
    1. dynamic error

instructs an Observable 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 Observable. 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 Stream. You can perform logic in the returnFn to return different Streams 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.


new Observable.error(new Exception())
  .onErrorReturnWith((e) => e is Exception ? 1 : 0)
  .listen(print); // prints 1


Observable<T> onErrorReturnWith(T Function(dynamic error) returnFn) =>
        (dynamic e) => Observable<T>.just(returnFn(e))));