WARNING: this code only works in very old versions of Dart. It does not work on Dart 1.0 or later.
As you note, in order to send messages to isolation, you need to have a handle on it sendport.
#import('dart:isolate'); main() { SendPort sendPort = spawnFunction(doWork); sendPort.call("hey 1").then((String res) => print("result was: [$res]")); sendPort.call("hey 2").then((String res) => print("result was: [$res]")); } doWork() { port.receive((msg, reply) { msg = "msg $msg"; reply.send(msg); }); }
however, since the main Dart stream itself is an isolate, you can send data to it using the global port function:
#import('dart:isolate'); #import('dart:io'); main() { port.receive((data, reply) { // in here you can access objects created in the main thread print("handle [${data['text']}] for index ${data['index']}"); }); SendPort workPort = spawnFunction(doWork); workPort.send("msg", port.toSendPort()); } doWork() { port.receive((msg, reply) { int i = 0; new Timer.repeating(1000, (Timer timer) { i++; var data = { "text": "$msg $i", "index": i }; print("sending $data"); reply.send(data); }); }); }
Please note that there are certain restrictions on what can be sent back and forth between isolates, and also currently isolates each other in JS and in the virtual machine. Current restrictions are well described here .
source share