tryCallSync<T> function

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

Tries to performa a call synchronously.

  • 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

T? tryCallSync<T>(T? Function() call,
    {T? defaultValue, T? onSuccessValue, T? onErrorValue}) {
  try {
    var ret = call();
    return onSuccessValue ?? ret ?? defaultValue;
  } catch (_) {
    return onErrorValue ?? defaultValue;
  }
}