invokeSafely<T> static method

Future<T?> invokeSafely<T>(
  1. FutureOr<T?> action(), {
  2. void onError(
    1. dynamic ex
    )?,
})

Invokes action, catching any exception it throws. If onError is given, it is called with the exception; otherwise the exception is silently ignored.

Implementation

static Future<T?> invokeSafely<T>(FutureOr<T?> Function() action,
    {void onError(ex)?}) async {
  try {
    return await action();
  } catch (ex) {
    onError?.call(ex);
  }
}