run<R, P> method

  1. @override
Future<R> run<R, P>(
  1. FutureOr<R>? function(
    1. P argument
    ),
  2. P argument, {
  3. Duration? timeout,
  4. FutureOr<R> onTimeout()?,
})
override

Execute function(argument) in the isolate and return the result.

Sends function and argument to the isolate, runs the call, and returns the result, whether it returned a value or threw. If the call returns a Future, the final result of that future will be returned.

If timeout is provided, and the returned future does not complete before that duration has passed, the onTimeout action is executed instead, and its result (whether it returns or throws) is used as the result of the returned future. If onTimeout is omitted, it defaults to throwing aTimeoutException.

This works similar to the arguments to Isolate.spawn, except that it runs in the existing isolate and the return value is returned to the caller.

Example:

IsolateRunner iso = await IsolateRunner.spawn();
try {
  return await iso.run(heavyComputation, argument);
} finally {
  await iso.close();
}

Implementation

@override
Future<R> run<R, P>(FutureOr<R>? Function(P argument) function, P argument,
    {Duration? timeout, FutureOr<R> Function()? onTimeout}) {
  return singleResultFuture<R>((SendPort port) {
    _commandPort.send(list4(_run, function, argument, port));
  }, timeout: timeout, onTimeout: onTimeout);
}