tryCall<T> function
FutureOr<T?>
tryCall<T>(
- FutureOr<
T?> call(), { - T? defaultValue,
- T? onSuccessValue,
- T? onErrorValue,
Tries to performa a call
.
- If
onSuccessValue
is defined it overwrites thecall
returned value. - If
onErrorValue
is defined it will be returned in case of error. - Returns
defaultValue
ifcall
returnsnull
andonSuccessValue
oronErrorValue
arenull
.
Implementation
FutureOr<T?> tryCall<T>(FutureOr<T?> Function() call,
{T? defaultValue, T? onSuccessValue, T? onErrorValue}) {
try {
return call().then((ret) => onSuccessValue ?? ret ?? defaultValue,
onError: (e) => onErrorValue ?? defaultValue);
} catch (_) {
return onErrorValue ?? defaultValue;
}
}