doneOnError method

Stream<T> doneOnError([
  1. bool predicate(
    1. Object e,
    2. StackTrace s
    )?
])

Returns a Stream that emits done event on first error event. The error is included in the output of the returned Stream.

The optional predicate function to determine whether to close on a given error and StackTrace. If predicate returns true, the returned Stream emits done event immediately and no other events will be forwarded. If null, a default predicate will be used, it will always return true.

Marble

input  : --a---b---c---x---d--|
output : --a---b---c---x|

NOTE: x is error event

Example

Rx.concat<int>([
  Stream.fromIterable([1, 2, 3]),
  Stream.error(Exception('1')),
  Stream.value(4),
  Stream.error(Exception('2')),
  Stream.fromIterable([5, 6, 7]),
])
    .doneOnError()
    .listen(print, onError: print); // prints 1, 2, 3, Exception: 1

Rx.concat<int>([
  Stream.fromIterable([1, 2, 3]),
  Stream.error(Exception('1')),
  Stream.value(4),
  Stream.error(Exception('2')),
  Stream.fromIterable([5, 6, 7]),
])
    .doneOnError((e, s) => e is Exception && e.toString() == 'Exception: 2')
    .listen(print,
        onError: print); // prints 1, 2, 3, Exception: 1, 4, Exception: 2

Implementation

Stream<T> doneOnError([bool Function(Object e, StackTrace s)? predicate]) =>
    Stream<T>.eventTransformed(
        this, (sink) => _DoneOnErrorSink<T>(sink, predicate ?? _alwaysTrue));