What are isolates in Dart?

Dart was built for constructing single-page applications. However, considering the prevalence of multicore processors in most computers, including mobile platforms, developers commonly utilize shared-memory threads that execute concurrently. Nonetheless, concurrency based on a shared state can lead to intricate and error-prone code. Hence, all code runs within isolates instead of threads in Dart.

Isolates function differently compared to threads. Each isolate has its memory heap, ensuring that the state of any isolate remains inaccessible from other isolates. They interconnect by passing messages through channels. Therefore, requiring a method to serialize messages. In Dart, isolates communicate through message passing as clients and servers.

Learn to create an isolate in Dart

We must add the following statement to our code to use isolates:

import 'dart:isolate';

We use Dart’s .spawn() method to build an isolate. The syntax for the method is shown below:

Isolate <isolateName> = await Isolate.spawn( enter_parameter );

The <parameter> represents the port receiving the message back.

Learn to kill an isolate in Dart

We use the .kill() method in Dart to kill an isolate. The syntax for the method is shown below:

isolateName.kill( <enter_parameter> );

Using spawn() and kill() methods together in a single program

Let’s look at the code snippet below, where we create an isolate using the .spawn() method, receive messages, and terminate the isolate using the .kill() method.

// First we'll import the dart libraries
import 'dart:io';
import 'dart:isolate';
import 'dart:async';
// Next, we create a new isolate
Isolate isolate;
void new_process() async
{
// We create a port for isolate to receive messages.
ReceivePort portReceive= ReceivePort();
// We start the isolate
isolate = await Isolate.spawn(func, portReceive.sendPort);
}
void func(SendPort sendPort)
{
int count = 0;
// We print the output message every 1 sec.
Timer.periodic(new Duration(seconds: 1), (Timer t) {
// We increase the counter
count++;
//Next, we print the output message
stdout.writeln('Welcome to Educative $count');
});
}
void end_process() {
// We'll check the isolate with null
if (isolate != null) {
stdout.writeln('Stop the Isolate');
// Killing the isolate
isolate.kill(priority: Isolate.immediate);
// Setting the isolate to null
isolate = null;
}
}
// Main Function
void main() async {
stdout.writeln('Start the isolate');
// We'll start the isolate with new_process
await new_process();
// Assuming that the process takes 3 seconds to finish
await Future.delayed(const Duration(seconds: 3));
// Here we call the end_process
end_process();
// Next we print the farewell message
stdout.writeln('Bye Bye!');
// Finally, we exit the program
exit(0);
}

Explanation:

  • Lines 2–4: We import Dart libraries into our code.
  • Line 7: We declare an isolate.
  • Line 13: We create a ReceivePort for the isolate to receive messages.
  • Line 16: We use the .spawn() method to instantiate the isolate with the receiving port created earlier.
  • Lines 18–30: We print output to the console every second.
  • Lines 32–43: We check if the isolate is not null. We terminate the isolate using the .kill() method.
  • Lines 46–63: We define the main function, which waits asynchronously for three seconds to let the new_process() print its messages to the console.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved