Recent documentation about Dart Isolates

橙三吉。 提交于 2019-12-05 08:29:19

I tried this example and it works https://gist.github.com/olostan/7883315

import "dart:isolate";

void main() {
  print("Starting");
  var sPort = new ReceivePort();
  SendPort rPort;
  sPort.listen((msg) {
    if (msg is SendPort) {
      print("Host got port. sending back");
      rPort = msg;
      rPort.send("Hello!");
    }
    else print("Host got $msg");
    rPort.send(null);
    sPort.close();
  });
  Isolate.spawn(test,sPort.sendPort);
}
void test(sender) {
  var rPort = new ReceivePort();
  sender.send(rPort.sendPort);
  rPort.listen((msg){
    print("Worker got $msg");
    if (msg!=null)
      sender.send("I am worker");
    else rPort.close();
  });
}

Isolates seem not to be used too much yet so there may still be some bugs.
The latest problems I remember reading about was debugging code running in isolates. I don't know if this is solved yet.
It also depends if you want to use isolates on the server or in the browser.
AFAIK it's more stable in the VM.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!