errorCallback method Null safety

AsyncError? errorCallback (
  1. Object error,
  2. StackTrace? stackTrace
)

Intercepts errors when added programmatically to a Future or Stream.

When calling Completer.completeError, StreamController.addError, or some Future constructors, the current zone is allowed to intercept and replace the error.

Future constructors invoke this function when the error is received directly, for example with Future.error, or when the error is caught synchronously, for example with Future.sync.

There is no guarantee that an error is only sent through errorCallback once. Libraries that use intermediate controllers or completers might end up invoking errorCallback multiple times.

Returns null if no replacement is desired. Otherwise returns an instance of AsyncError holding the new pair of error and stack trace.

Although not recommended, the returned instance may have its error member (AsyncError.error) be equal to null in which case the error should be replaced by a NullThrownError.

Custom zones may intercept this operation.

Implementations of a new asynchronous primitive that converts synchronous errors to asynchronous errors rarely need to invoke errorCallback, since errors are usually reported through future completers or stream controllers.

Implementation

AsyncError? errorCallback(Object error, StackTrace? stackTrace);