create<Q, R> static method
Future<IsolateFlutter?>
create<Q, R>(
- ComputeCallback<
Q, R> callback, - Q message, {
- String? debugLabel,
Create an isolate. The isolate will start up in a paused state. To start and get the return value, call isolateFlutter.start()
.
Return the created isolate.
Run callback
on that isolate, passing it message
, and (eventually) return the value returned by callback
.
The callback
argument must be a top-level function, not a closure or an instance or static method of a class.
Q
is the type of the message that kicks off the computation.
R
is the type of the value returned.
The debugLabel
argument can be specified to provide a name to add to the Timeline.
This is useful when in debuggers and logging.
Implementation
static Future<IsolateFlutter?> create<Q, R>(
ComputeCallback<Q, R> callback, Q message,
{String? debugLabel}) async {
final _isolateFlutter = IsolateFlutter._();
if (_isolateFlutter._status != null) {
// _isolateFlutter has been created
return null;
}
final _debugLabel =
debugLabel ?? 'IsolateFlutter_${_isolateFlutter.hashCode}';
// Create Completer
_isolateFlutter._completer = Completer<dynamic>();
// Create ReceivePort
_isolateFlutter._port = RawReceivePort((dynamic msg) {
_isolateFlutter._completer.complete(msg);
_isolateFlutter.stop();
}, _debugLabel);
// Swap an isolate
try {
final _sendPort = _isolateFlutter._port.sendPort;
_isolateFlutter._isolate = await Isolate.spawn(
_spawn,
IsolateFlutterConfiguration<Q, FutureOr<R>>(
callback, message, _sendPort, _debugLabel),
errorsAreFatal: true,
onError: _sendPort,
onExit: _sendPort,
debugName: _debugLabel,
paused: true);
} catch (e) {
throw RemoteError('IsolateFlutter cann`t be created.', '');
}
_isolateFlutter._status = IsolateFlutterStatus.Paused;
return _isolateFlutter;
}