create static method

Creates a session in a background isolate.

If OrtIsolateSessionConfig.providers is set, those providers are used. Otherwise, providers are auto-selected for the current platform.

The model is loaded inside the isolate — this call is fully async and will not block the UI thread.

Implementation

static Future<OrtIsolateSession> create(
  OrtIsolateSessionConfig config,
) async {
  final receivePort = ReceivePort();
  final broadcastStream = receivePort.asBroadcastStream();

  final isolate = await Isolate.spawn(
    _workerEntryPoint,
    _WorkerInit(sendPort: receivePort.sendPort, config: config),
  );

  // Phase 1: worker sends its SendPort
  final workerPort = await broadcastStream.first as SendPort;

  // Phase 2: worker sends _ReadyResponse or _ErrorResponse
  final readyMsg = await broadcastStream.first;
  if (readyMsg is _ErrorResponse) {
    receivePort.close();
    isolate.kill();
    throw Exception(readyMsg.message);
  }

  final ready = readyMsg as _ReadyResponse;
  return OrtIsolateSession._(
    isolate: isolate,
    workerPort: workerPort,
    responses: broadcastStream,
    inputNames: ready.inputNames,
    outputNames: ready.outputNames,
  );
}