tryCallMapped<T, R> function
FutureOr<R?>
tryCallMapped<T, R>(
- FutureOr<
T?> call(), { - R? defaultValue,
- R? onSuccessValue,
- R? onSuccess(
- T? value
- R? onErrorValue,
- R? onError(
- Object error,
- StackTrace s
Tries to performa a call.
See tryCall.
Implementation
FutureOr<R?> tryCallMapped<T, R>(
FutureOr<T?> Function() call, {
R? defaultValue,
R? onSuccessValue,
R? Function(T? value)? onSuccess,
R? onErrorValue,
R? Function(Object error, StackTrace s)? onError,
}) {
try {
return call().then(
(ret) {
if (onSuccess != null) {
return onSuccess(ret) ?? onSuccessValue ?? defaultValue;
}
return ret.resolveMapped(
(r) => onSuccessValue ?? r as R? ?? defaultValue,
);
},
onError: (e, s) {
if (onError != null) {
return onError(e, s) ?? onErrorValue ?? defaultValue;
}
return onErrorValue ?? defaultValue;
},
);
} catch (e, s) {
if (onError != null) {
return onError(e, s) ?? onErrorValue ?? defaultValue;
}
return onErrorValue ?? defaultValue;
}
}