tryCall<T> function

FutureOr<T?> tryCall<T>(
  1. FutureOr<T?> call(), {
  2. T? defaultValue,
  3. T? onSuccessValue,
  4. T? onErrorValue,
})

Tries to performa a call.

  • If onSuccessValue is defined it overwrites the call returned value.
  • If onErrorValue is defined it will be returned in case of error.
  • Returns defaultValue if call returns null and onSuccessValue or onErrorValue are null.

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;
  }
}