Dart isolate call or send

Issue

I have a couple questions about how isolate works :

1) What is the difference between call and send and when I should use call over send?

2) Just curiosity, is there any way to chain isolate like we chain Future ?

3)

import 'dart:isolate';
echo() {
  port.receive((msg, reply) {
    print('I received: $msg');
  });
}

main() {
  var sendPort = spawnFunction(echo);
  sendPort.call('Hello from main');
}

It displays : I received: Hello from main

but when I use send, it prints nothing, why?

Solution

Use the call() method on SendPort as a simple way to send a message and receive a reply. The call() method returns a Future for the reply. If you don’t bother of the reply and simply want to send a message, use send().

Have a look at dart:isolate – Concurrency with Isolates for more informations.

For 3) it’s explained in the above link :

In the standalone VM, the main() function runs in the first isolate (also known as the root isolate). When the root isolate terminates, it terminates the whole VM, regardless of whether other isolates are still running. For more information, see the section called “Keeping the root isolate alive”.

Answered By – Alexandre Ardhuin

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

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