from<T extends Object?, S extends T?> static method
Future<AsyncPhase<T> >
from<T extends Object?, S extends T?>(
- FutureOr<
T> func(), { - S? fallbackData,
- void onComplete(
- T
- void onError(
- S?,
- Object,
- StackTrace
A method that runs an asynchronous function and returns either AsyncComplete with the function result as data or AsyncError with the error information, depending on whether or not the function completed successfully.
If the asynchronous function resulted in an error,
the fallbackData
value is used as the data of AsyncError.
The onError
callback is called on error. This may be
useful for logging.
Implementation
static Future<AsyncPhase<T>> from<T extends Object?, S extends T?>(
FutureOr<T> Function() func, {
// `S` is a subtype of `T?`, but this parameter must not be of type
// `T?`, in which case this method returns an `AsyncPhase<T?>` in
// stead of `AsyncPhase<T>` if `null` is passed in.
S? fallbackData,
void Function(T)? onComplete,
void Function(S?, Object, StackTrace)? onError,
}) async {
try {
final data = await func();
onComplete?.call(data);
return AsyncComplete(data);
}
// ignore: avoid_catches_without_on_clauses
catch (e, s) {
onError?.call(fallbackData, e, s);
return AsyncError(data: fallbackData, error: e, stackTrace: s);
}
}