Is it possible to have multiple send and receive ports for Dart Isolates?

Issue

Is it possible to open a multiple send and receive ports for the same Isolate in Dart?

E.g. Following code sample would have created two isolates with each having its own send port. However, I was wondering if there is any way to create more than one send/receive ports for the same isolate and choose the receive port to send the message to.

    #import('dart:isolate');

    echo() {

    }

    main() {
        var sendPort1 = spawnFunction(echo);
        var sendPort2 = spawnFunction(echo);
    }

Solution

While I’m not sure about multiple receive ports. You can create multiple send ports per receive port. This functionality is build into the ReceivePort class: ReceivePort.toSendPort

As indicated at the bottom of the help:

It is legal to create several SendPorts from the same ReceivePort.

Hope this helps.

Answered By – Matt B

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *