attach static method

  1. @experimental
IsolateClient attach(
  1. SendPort commandSendPort
)

Create a new IsolateClient by attaching to an already existing Client running in another Isolate.

This is intended for the 'Add-to-app' use case and especially the Multiple Flutters scenario where each Flutter instance is run in a separate isolate.

After the IsolateClient has been created in the first FlutterEngine the commandSendPort can be stored in an IsolateNameServer. In subsequent engines from the same FlutterEngineGroup, the port can be looked up from the IsolateNameServer and supplied to this function.

Example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final gqlClient = await IsolateClient.create(
    ...
    messageHandler: (msg) {
      ...
    }
  );
  IsolateNameServer.registerPortWithName(gqlClient.commandSendPort, "name");
}

@pragma("vm:entry-point")
void other() async {
  WidgetsFlutterBinding.ensureInitialized();

  final port = IsolateNameServer.lookupPortByName("name")!;
  final gqlClient = IsolateClient.attach(port);

  runApp(...)
}

NOTE: all messages from the Client will need to be handled by the messageHandler in the Isolate where the client was initially created.

Implementation

@experimental
static IsolateClient attach(SendPort commandSendPort) {
  final client = IsolateClient._();
  client.commandSendPort = commandSendPort;
  client._messageHandlerReceivePort = null;
  return client;
}