W3cubDocs

/Dart 2

Future<T>.error constructor

Future<T>.error(Object error, [ StackTrace stackTrace ])

Creates a future that completes with an error.

The created future will be completed with an error in a future microtask. This allows enough time for someone to add an error handler on the future. If an error handler isn't added before the future completes, the error will be considered unhandled.

If error is null, it is replaced by a NullThrownError.

Use Completer to create a future and complete it later.

Implementation

factory Future.error(Object error, [StackTrace stackTrace]) {
  error = _nonNullError(error);
  if (!identical(Zone.current, _rootZone)) {
    AsyncError replacement = Zone.current.errorCallback(error, stackTrace);
    if (replacement != null) {
      error = _nonNullError(replacement.error);
      stackTrace = replacement.stackTrace;
    }
  }
  return new _Future<T>.immediateError(error, stackTrace);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-async/Future/Future.error.html