onErrorReturnWith method

Single<T> onErrorReturnWith(
  1. T itemSupplier(
    1. Object error,
    2. StackTrace stackTrace
    )
)

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

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

The itemSupplier receives the emitted error and returns a value. You can perform logic in the itemSupplier 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.

Example

Single<int>.error(Exception())
  .onErrorReturnWith((e, s) => e is Exception ? 1 : 0)
  .listen(print); // prints 1

Implementation

Single<T> onErrorReturnWith(
        T Function(Object error, StackTrace stackTrace) itemSupplier) =>
    Single.safe(Stream<T>.eventTransformed(
        this, (sink) => _OnErrorReturnWithSingleSink<T>(sink, itemSupplier)));