Flutter Isolates Part — 1

Kunal Jain
tech@iiit-gwalior
Published in
4 min readJun 14, 2021

I was recently working with two of my friends (Harsh and Aaryak) on a Flutter App. We needed to create a system which would allow the user’s data to be synced across all their devices, taking into account their network connection. We decided to implement this using Flutter Isolates. This blog post is the first in the series, explaining the process of how we approached that thought of building a sync engine. In this blog, I will try to give you a good overview of what Isolates in Flutter are, how are they useful, and what are the possible scenarios where you can think of using them.

So let’s begin with the very first question →

What are Flutter Isolates?

So to help you understand the concept of Isolates better, I’ll try to explain them using an analogy. Consider you have a program that is completely , well, ‘isolated’ from the rest of the surroundings with a memory of its own, and an event loop of its own. If you don’t know what an event loop, don’t sweat it; here’s the tl;dr — an event loop can be imagined as a conveyor belt taking tasks from one end, one at a time (since Dart is single-threaded), performing those tasks, and throwing them away, returning any output of the task if it exists, from the other end of the belt. Now, coming back to Isolates. When you run a Flutter app it runs inside an isolate with a memory and event loop of its own. The best part is that these isolates are ‘isolated’, for the lack of a better word, from each other. That is, an isolate doesn’t have access to the memory of any other isolate. Moreover, even if you request to create an Isolate from inside of another Isolate (we’ll see how we perform this action in later parts of this series), the parent Isolate does not have access to the memory of the child Isolate (and vice versa).

How are Isolates useful and where to use them?

Now that we know what Isolates are, let us understand how are they useful to us. Consider a scenario where you’re building a Flutter application that requires some heavy computation to be done whenever a user signs in. If you carry out that computation on the main isolate (that is, the isolate in which your app is running), your application will just freeze as soon as you initiate that computation. This is because Dart is single-threaded, and so, the event loop will start performing that computation and will wait to perform any other task before that computation ends. In such scenarios, you can use the amazing superpower of spawning an Isolate from the main Isolate and ask it to perform that heavy computation for you. As soon as it completes that heavy computation, you can ask it to return the result of that computation back to the main Isolate.

But wait… Didn’t I say that Isolates are isolated from each other? So how can the result of the computation be returned from the child to the parent?

Yes, I did mention that Isolates cannot access the memory of other Isolates — but I never mentioned that they can’t talk to each other. Okay, so at this point it might be fair to say tha you’re a bit confused. Let me clear up the definition of isolates for you once again — “Isolates are programs which are isolated from each other and have their own memory and event loop which can’t be accessed by any other Isolate, but you can communicate between Isolates by sending and receiving messages through ports.”

I’ll cover what these ports are, how the communication is established, and more importantly, how do we actually code all of this, in the next part of this series. Till then, you can learn more about Isolates using these resources:

A really good YouTube video on Isolates by Flutter Melbourne: Featherweight Isolates in Flutter

Official Docs: https://api.flutter.dev/flutter/dart-isolate/Isolate-class.html

Official YouTube channel of Flutter: Isolates and Event Loops — Flutter in Focus

--

--

Kunal Jain
tech@iiit-gwalior

SDE Intern at KoinX | Ex-FamPay | Ex-Trell | IIIT Gwalior'24