runOnPlatformThread<R> function
- FutureOr<
R> computation()
Runs computation
on the platform thread and returns the result.
This may run the computation on a separate isolate. That isolate will be reused for subsequent runOnPlatformThread calls. This means that global state is maintained in that isolate between calls.
The computation
and any state it captures may be sent to that isolate.
See SendPort.send for information about what types can be sent.
If computation
is asynchronous (returns a Future<R>
) then
that future is awaited in the new isolate, completing the entire
asynchronous computation, before returning the result.
If computation
throws, the Future
returned by this function completes
with that error.
The computation
function and its result (or error) must be
sendable between isolates. Objects that cannot be sent include open
files and sockets (see SendPort.send for details).
This method can only be invoked from the main isolate.
This API is currently experimental.
Implementation
Future<R> runOnPlatformThread<R>(FutureOr<R> Function() computation) {
if (!_platformIsolatesEnabled) {
throw UnsupportedError(
'Platform thread isolates are not supported by this platform.');
}
if (isRunningOnPlatformThread) {
return Future<R>(computation);
}
final SendPort? sendPort = _platformRunnerSendPort;
if (sendPort != null) {
return _sendComputation(sendPort, computation);
} else {
return (_platformRunnerSendPortFuture ??= _spawnPlatformIsolate())
.then((SendPort port) => _sendComputation(port, computation));
}
}