Dart Isolate 2-Way Communication

Leland Zach
2 min readAug 6, 2019
Dart Thread 2-Way Communication
Spawn Threads using the Dart or Flutter Isolate Class

The Dart programming language allows programmers to write code that executes asynchronously. This allows Dart 2.4.0 (and also Flutter) to do things like execute HTTP requests, and patiently wait for a response before continuing to execute the proceeding code.

Dart also allows a programmer to execute a function asynchronously in a separate thread! This can be done using the Isolate class, which is in a native Dart library. A Dart Isolate is an object that executes a specific function in a newly spawned thread. The second thread can communicate with the main thread continuously.

Async Dart Isolates

The Isolate class was designed for message passing between threads using a non-broadcast streaming object called a ReceivePort. What this means is a ReceivePort can be listened to just once. A contrasting method in the ReceivePort object is called the SendPort.

ReceivePorts are for listening for messages, and SendPorts are for sending messages.

Dart Thread Code Example

In order to make async threads with Isolates, you need import 2 native Dart libraries. No need to install them, they come with Dart.

--

--