Flutter › Flutter Isolates & Multithreading

How do you properly establish genuine ongoing bidirectional communication between the main isolate and a manually spawned separate isolate using SendPort and ReceivePort, for a scenario requiring more than just a single one off request and response?

Intermediate

Answer

For a scenario genuinely requiring more than just a single simple one off request and corresponding response, such as a long running background isolate that needs to continuously send periodic progress updates back to the main isolate, you manually create a ReceivePort on each individual side, then properly exchange each other's corresponding SendPort references, letting either isolate send a genuine message to the other at essentially any point simply by calling send on that specific other isolate's own SendPort, establishing a fully working genuine bidirectional communication channel that can remain properly open and continue in active use for as long as it is genuinely still actually needed.
final mainReceivePort = ReceivePort();
final isolate = await Isolate.spawn(workerFunction, mainReceivePort.sendPort);
mainReceivePort.listen((message) => print('Received: $message'));
Real-world example A large file processing background isolate sends periodic progress update messages back to the main isolate throughout an ongoing lengthy operation, letting the main isolate correctly update a visible progress bar in real time as that background work genuinely continues to steadily proceed.

Common follow-ups: How do you actually properly close down and clean up a ReceivePort once it is genuinely no longer needed?;What specific types of data can and cannot actually be sent as a message between two separate isolates?