Is there any example for dart's `spawnUri(…)` in library "dart:isolate"?

Issue

There is a spawnUri(uri) function in dart:isolate, but I don’t find any example. I have guessed its usage, but failed.

Suppose there are 2 files, in the first one, it will call spawnUri for the 2nd one, and communicate with it.

first.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());
}

second.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() {}

But this example doesn’t work. I don’t know what’s the correct code, how to fix it?

Solution

WARNING : This code is out of date.

Replace your second.dart with the following to make it work :

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])));
}

Answered By – Alexandre Ardhuin

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

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