runIsolateTask<T> method

Future<T?> runIsolateTask<T>(
  1. FutureOr<T> task(), {
  2. String? id,
  3. TaskPriority priority = TaskPriority.normal,
  4. int retries = 0,
  5. Duration? retryDelay,
  6. bool useExponentialBackoff = true,
  7. double weight = 1.0,
  8. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  9. TaskCachePolicy<T>? cachePolicy,
  10. String? debugName,
})

Executes a task in a separate Isolate using Isolate.run.

This is ideal for heavy computational tasks that would otherwise block the main isolate. It leverages runTask internally for queueing, reactive status tracking, and retries.

Constraint: The task must be a top-level function or a static method. Closures that capture state cannot be sent across isolate boundaries.

debugName is an optional name for the isolate, visible in debug tools.

Implementation

Future<T?> runIsolateTask<T>(
  FutureOr<T> Function() task, {
  String? id,
  TaskPriority priority = TaskPriority.normal,
  int retries = 0,
  Duration? retryDelay,
  bool useExponentialBackoff = true,
  double weight = 1.0,
  void Function(Object error, StackTrace stackTrace)? onError,
  TaskCachePolicy<T>? cachePolicy,
  String? debugName,
}) {
  return runTask(
    () => Isolate.run(task, debugName: debugName),
    id: id,
    priority: priority,
    retries: retries,
    retryDelay: retryDelay,
    useExponentialBackoff: useExponentialBackoff,
    weight: weight,
    onError: onError,
    cachePolicy: cachePolicy,
  );
}