handleError method
Creates a wrapper Stream that intercepts some errors from this stream.
If this stream sends an error that matchestest, then it is interceptedby theonError function.
TheonError callback must be of typevoid Function(Object error) orvoid Function(Object error, StackTrace).The function type determines whetheronError is invoked with a stacktrace argument.The stack trace argument may beStackTrace.empty if this stream receivedan error without a stack trace.
An asynchronous errorerror is matched by a test function iftest(error) returns true. Iftest is omitted, every error isconsidered matching.
If the error is intercepted, theonError function can decide what to dowith it. It can throw if it wants to raise a new (or the same) error,or simply return to make this stream forget the error.If the receivederror value is thrown again by theonError function,it acts like arethrow and it is emitted along with its originalstack trace, not the stack trace of thethrow insideonError.
If you need to transform an error into a data event, use the more genericStream.transform to handle the event by writing a data event tothe output sink.
The returned stream is a broadcast stream if this stream is.If a broadcast stream is listened to more than once, each subscriptionwill individually perform thetest and handle the error.
Example:
Stream.periodic(const Duration(seconds: 1), (count) { if (count == 2) { throw Exception('Exceptional event'); } return count;}).take(4).handleError(print).forEach(print);// Outputs:// 0// 1// Exception: Exceptional event// 3// 4Implementation
Stream<T> handleError(Function onError, {bool test(error)?}) { final void Function(Object, StackTrace) callback; if (onError is void Function(Object, StackTrace)) { callback = onError; } else if (onError is void Function(Object)) { callback = (Object error, StackTrace _) { onError(error); }; } else { throw ArgumentError.value( onError, "onError", "Error handler must accept one Object or one Object and a StackTrace" " as arguments.", ); } return _HandleErrorStream<T>(this, callback, test);}