What, When, and Where to Use Isolates in Flutter

Agnel Selvan
3 min readApr 23, 2024

--

In this article, we will look into what is single and multi-threading and will cover topics on What, Where, and When to use Isolates with an example.

Before jumping into Isolates, let’s first understand what single-threading and multi-threading are in Mobile Applications.
Single-Threading means executing only one instruction at a time.
If the single instruction execution time is in milliseconds, it won’t affect much. Now what if some instruction execution time is quite more let’s assume 2–3 seconds, this will cause UI to freeze in Flutter. So to execute intense calculations it’s recommended to execute the intense calculation on a different thread and then get the calculated data to the main thread.
Multi-threading means executing instructions concurrently on a different thread. In Flutter this Multi-threading can be achieved using Isolates.

What are Isolates?

The process of executing multiple tasks on a different thread is called Parallelism and this Parallelism can be achieved using Isolates. With the help of Isolate.spawn, Isolate.exit, ReceivePort, SendPort we can achieve Parallelism in Flutter.
This same Parallelism can also be achieved using compute. Flutter Web doesn’t support Isolates. So, it's recommended to use compute instead of Isolates on Flutter Web.

When to Use Isolates?

Isolates are used in places when intense calculation tasks cause UI jank. Using Isolates, multiple independent tasks are performed concurrently and each Isolates has his own memory.

Where to Use Isolates?

In case you are performing a search and sorting operation from a large chunk of data, there these isolates will be useful. Performing the searching, and sorting of data from a background thread using Isolate.spawn and getting the data to the main thread using ReceivePort and SendPort.

Examples

One of my more recent projects which I’m currently working on, faced an issue when loading huge data from a Local Database and applying sorting and searching operations. Here sorting and searching operations are not only done as per the basis 781 Products, operations are done along with a bunch of categories as well as with companies. I solved this UI Jank issue with the help of Isolates.

This is an example of loading a bunch of data without using Isolates

Without Using Isolates

This is an example of loading a bunch of data using Isolates

Using Isolates

Here is an example of how I used Isolate to spawn, and pass the required details and get to the main thread using Isolate.exit, close the ReceivePort, and kill the Isolate when not in use.

Do you know you can press the clap👏 button 50 times? The higher you go, the more it motivates me to write more stuff for you guys!
Hello, I am Agnel Selvan. A noob developer. You can find me on Linkedin or stalk me on GitHub.

If you like my work or the free stuff on this website and want to say thanks or encourage me to do more, you can buy me a coffee!

--

--

Agnel Selvan

A person who is learning Flutter Development, Shaders, OpenGL, and Augmented Reality.