sendToPort static method

Future<void> sendToPort(
  1. dynamic message
)

Sends a message to the other isolate, which is handled by whatever function was passed to setupIsolateCommunication in that isolate

i.e. background_sendToPort("a") -> main_receiveHandler("a")

Values that can be sent are subject to the limitations of SendPort, i.e. primitives and lists/maps thereof

Implementation

static Future<void> sendToPort(dynamic message) async {
  final SendPort? targetPort = IsolateNameServer.lookupPortByName(
      (await isBackgroundIsolate
          ? _MAIN_ISOLATE_PORT_NAME
          : _BACKGROUND_ISOLATE_PORT_NAME));

  if (targetPort != null) {
    targetPort.send(message);
  } else {
    throw SendToPortException(await isBackgroundIsolate);
  }
}