Orchestrating Concurrent Processes with Isolates — Flutter

Lakshay Parnami
2 min readMar 7, 2023

Flutter isolates

This blog on Isolates is pleased to welcome you! We’ll talk about isolates in Dart and how Flutter uses them to build scalable and responsive applications in this blog.

Let’s start by explaining what isolates in Dart are. An isolate in Dart is a lightweight concurrent process that executes independently of the main program thread. Isolates can communicate with one another through message passing and have their own memory heap. As a result, isolates can execute code concurrently without interfering with one another’s memory access.

In Flutter, how are isolates used? Isolates are used in Flutter to build responsive user interfaces and enhance application performance. By performing pricey tasks, such as network requests or intensive

First, we define a function that will run in the isolate:

void getDataFromServer(SendPort sendPort) async {
// Send the data to the main thread through the sendPort
sendPort.send(await makeNetworkRequest());
}

A SendPort is a parameter that this method accepts and uses to interact with the main thread. The data is sent back to the main thread via the sendPort after being retrieved from the server using the makeNetworkRequest() function.

Next, we define the function that will be called when the button is clicked:

void getData() async {
// Create a new isolate and get its receive port
final receivePort = ReceivePort();
await Isolate.spawn(getDataFromServer, receivePort.sendPort);

// Wait for the data to be received from the isolate
final data = await receivePort.first;

// Update the UI with the retrieved data
setState(() {
_data = data;
});
}

The getDataFromServer() function and the receive port of the main thread are passed to the newly created isolate created by this function using the Isolate.spawn() function. Following that, it changes the UI with the retrieved data after waiting for the data to be received from the isolate through the receivePort.

The main UI thread is kept free to handle user interactions and maintain the responsiveness of the program by processing the network request in a separate isolate.

Isolates are a strong tool in the Dart programming language, and Flutter makes considerable use of them to build scalable and responsive apps. The main UI thread is kept free to handle user interactions by conducting expensive processes in separate isolates, creating a smoother and more responsive user experience.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Lakshay Parnami
Lakshay Parnami

Written by Lakshay Parnami

Hi, I’m Lakshay Parnami. I’m into Mobile Application Development, I work on Android, Flutter & React Native. I’m currently learning iOS development.

Responses (1)

Write a response

Isolate.run() makes most of this unnecessary, and works just as easily in Dart as in Flutter.