spawn<T> static method
Creates and spawns a flutter isolate that shares the same code as the current isolate. The spawned isolate will be able to use flutter plugins. T can be any type that can be normally be passed through to regular isolate's entry point.
Implementation
static Future<FlutterIsolate> spawn<T>(
void entryPoint(T message), T message) async {
final userEntryPointId =
PluginUtilities.getCallbackHandle(entryPoint)!.toRawHandle();
final isolateId = Uuid().v4();
final isolateResult = Completer<FlutterIsolate>();
final setupReceivePort = ReceivePort();
IsolateNameServer.registerPortWithName(
setupReceivePort.sendPort, isolateId);
late StreamSubscription setupSubscription;
setupSubscription = setupReceivePort.listen((data) {
final portSetup = (data as List<dynamic>);
final setupPort = portSetup[0] as SendPort;
final remoteIsolate = FlutterIsolate._(
isolateId, portSetup[1] as SendPort?, portSetup[2], portSetup[3]);
setupPort.send(<Object?>[userEntryPointId, message]);
setupSubscription.cancel();
setupReceivePort.close();
isolateResult.complete(remoteIsolate);
});
_control.invokeMethod("spawn_isolate", {
"entry_point":
PluginUtilities.getCallbackHandle(_flutterIsolateEntryPoint)!
.toRawHandle(),
"isolate_id": isolateId
});
return isolateResult.future;
}