isolate<T> method

TaskHandle<T> isolate<T>(
  1. FutureOr<T> computation(), {
  2. int priority = 0,
  3. Duration? timeout,
  4. TaskTimeouts? timeouts,
  5. RetryPolicy? retry,
})

Executes a computation in a separate isolate with concurrency limit

This method wraps Isolate.run and executes it through the FLimit instance. The computation runs in a separate isolate, preventing blocking of the main thread, while still respecting the concurrency limit.

The computation function must be a top-level function, a static method, or a closure that captures only sendable state from the current isolate.

priority can be used to set the priority of the task if the queue strategy supports it. Higher values execute first. Defaults to 0.

timeout if specified, the task will fail with TimeoutException if it doesn't complete within the given duration. This is an alias for timeouts.run.

timeouts provides queue/run/total timeout control. If timeouts.run is specified, do not also pass timeout.

retry if specified, failed tasks will be retried according to the policy. Note: Retry with isolate may have limited usefulness since the computation runs in a separate isolate and errors may not be easily recoverable.

Returns a TaskHandle that can be used to cancel the task or await the result.

Example:

int calculateSquare(int n) => n * n;

void main() async {
  final limit = fLimit(2);

  final result = await limit.isolate(() => calculateSquare(42));
  print(result);
}

See also:

  • Isolate.run for more information about isolate constraints

Implementation

TaskHandle<T> isolate<T>(
  FutureOr<T> Function() computation, {
  int priority = 0,
  Duration? timeout,
  TaskTimeouts? timeouts,
  RetryPolicy? retry,
}) {
  return this(
    () => Isolate.run(computation),
    priority: priority,
    timeout: timeout,
    timeouts: timeouts,
    retry: retry,
  );
}