tryCallMapped<T, R> function

FutureOr<R?> tryCallMapped<T, R>(
  1. FutureOr<T?> call(), {
  2. R? defaultValue,
  3. R? onSuccessValue,
  4. R? onSuccess(
    1. T? value
    )?,
  5. R? onErrorValue,
  6. R? onError(
    1. Object error,
    2. 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;
  }
}