spawnUri(uri)
中有一个 dart:isolate
函数,但我没有找到任何示例。我猜到了它的用法,但失败了。
假设有2个文件,在第一个文件中,它会为第二个文件调用spawnUri
,并与之通信。
首先.dart
import "dart:isolate";
main() {
ReceivePort port = new ReceivePort();
port.receive((msg, _) {
print(msg);
port.close();
});
var c = spawnUri("./second.dart");
c.send(["Freewind", "enjoy dart"], port.toSendPort());
}
第二个.dart
String hello(String who, String message) {
return "Hello, $who, $message";
}
void isolateMain(ReceivePort port) {
port.receive((msg, reply) => reply.send(hello(msg[0], msg[1]));
}
main() {}
但是这个例子不起作用。我不知道正确的代码是什么,如何解决?
最佳答案
警告:此代码已过时。
将您的 second.dart 替换为以下内容以使其工作:
import "dart:isolate";
String hello(String who, String message) {
return "Hello, $who, $message";
}
main() {
port.receive((msg, reply) => reply.send(hello(msg[0], msg[1])));
}
关于dart - `spawnUri(...)` 库中是否有 dart 的 "dart:isolate"示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299512/