runTask<T> static method

Future<void> runTask<T>({
  1. required BuildContext context,
  2. required AsyncValueGetter<T> task,
  3. FetcherConfig? config,
  4. AsyncValueSetter<T>? onSuccess,
})

Safely run a async task. Headless version of AsyncTaskBuilder.

Implementation

static Future<void> runTask<T>({
  required BuildContext context,
  required AsyncValueGetter<T> task,
  FetcherConfig? config,
  AsyncValueSetter<T>? onSuccess,
}) async {
  try {
    // Run task
    final result = await task();

    // Success callback
    await onSuccess?.call(result);
  } catch(e, s) {
    // Get default config if context is mounted
    if (context.mounted) {
      config = DefaultFetcherConfig.of(context).apply(config);
    }

    // Error callback
    config?.onError?.call(e, s);

    // Display error callback
    if (context.mounted) config?.onDisplayError?.call(context, e);
  }
}