Async vs. Isolate in Flutter: Parallelism in Flutter

Aryan Bisht
5 min readApr 21, 2024

--

Heading Image

There is a lot of confusion between the idea of async and isolates OR (concurrency and parallelism). Today in this article we will be clearing these confusion and in up-coming article I will be implementing isolates in Dart so make sure to follow me for future articles. Now, let’s start.

I am posting a video too which will help you to understand the difference between async and isolate.

ASYNC

First I will be explaining about async as most of us have used this at-least once.

void readData() async {
var url = "www.example.com";
var content = await http.get(url); //Async Call
var data = parsingData(content); // Will use isolate here as computation heavy
}

It runs normally, and then it waits while a file is being downloaded. While it waits, other parts of our program can run. Notice how there’s always only one part of the app running at any given time. After all, we’re running on only one thread, one processor. Async doesn’t automatically make your app multithreaded. After the file is downloaded, the method will continue. That’s async. And when that future completes, it continues the run from the next line. Most of the time, async is all you need.

In the above example our code will wait till the 2nd line ( await http.get(url) ) and wait till all data gets fetched from the internet and after that we will parse the data accordingly.

Let’s say the file is huge, and parsing it requires a lot of processor power. Will this make our app stutter? You might think, no, it won’t. The computation happens in an async method, right? But unfortunately, you’re wrong. Yes! using an async method might cause lag or stutter in your app.

You might wonder why this is happening when parsing is inside an async block. Because even though the method is asynchronous, a large chunk of synchronous work is happening inside it. That parsing is synchronous. It’s happening on your thread. That will keep the processor busy, and so no other part of your app will be running. The UI won’t refresh until the heavy work is finished, and the user will see stutter

So what’s the solution? Multithreading with isolates!

ISOLATES

Dart allows you to run code in parallel via isolates. With an isolate, you start another Dart program, basically, on a different thread, on a different processor. The two separate isolates are isolated:-) . One will not block the other. This way, your app can do heavy processing while also running smoothly for the user. I won’t get into how to implement isolates here. It’s a bit more involved, and this article is only meant to distinguish between the two concepts of async versus isolates.

Isolate like a little space on the machine with its own private chunk of memory and a single thread running an event loop. In a lot of other languages like C++, you could have multiple threads sharing the same memory and running whatever code they want. In Dart, though, each thread is in its own isolate with its own memory and it just processes events.

Why and when to use Isolates?

Many Dart apps run all their code in a single isolate, but you can have more than one if you need it. If you have a computation to perform that’s so enormous it could cause you to drop frames if it were run in the main isolate, you can use Isolate.spawn() or Flutter’s compute function, both of which create a separate isolate to do the number crunching, leaving your main one free to rebuild and render the widget tree in the meantime. That new isolate will get its own event loop and its own memory, which the original isolate, even though it’s the parent of this new one, isn’t allowed to access. That’s the source of the name isolate. These little spaces are kept isolated from one another. In fact, the only way they can work together is by passing messages back and forth. One isolate will send a message to the other, and that receiving isolate process is the message using its event loop.

This lack of shared memory may sound kind of strict, especially if you’re coming from a language like Java or C++, but it has some key benefits for Dart coders. For example, memory allocation and garbage collection in an isolate don’t require locking. There’s only one thread, so if it’s not busy, you know the memory’s not being mutated. That works out really well for Flutter apps, which sometimes need to build up and tear down a bunch of widgets really quickly. All right. So that’s a basic introduction to isolates.

For implementation check out my latest article about isolate. I have shared github repo for the project also : — LINK

Summary

Let’s summarize. Async is the ability to wait for other things without blocking. Async will not create a separate thread for you. Dart will just jump between the different parts of your program as needed. In most cases, that’s fine. It’s like if I talk to you and also sometimes check my phone. When I’m checking my phone quickly, it’s fine — kind of annoying, but fine. Right? Our conversation still flows. But if I suddenly start staring at the screen and ignoring you then that’s not great. That’s async. Isolates give you the ability to run things in parallel. One isolate doesn’t care what the other one is doing at any given time. They can both run at full speed. Isolates are harder to set up, but it’s also the only way to deal with some situations, like the parsing example I mentioned. Isolates are like when I talk to you and also play around with Dash here. I can do those things in parallel. All right, I hope that was helpful.

Isolates play a crucial role in achieving concurrent processing. Performing resource-intensive operations in the main thread can have a negative impact on the application’s performance. As the time available for each frame decreases with higher frame rates, tasks that exceed the available time should be executed in separate isolates to maintain smooth rendering.

Although isolates come with some limitations on the size of transmitted data, utilizing them appropriately can significantly enhance the performance and efficiency of Flutter applications.

More from Aryan Bisht

You can buy me coffee at :- https://www.buymeacoffee.com/aryanbisht

Level Up Coding And Development Journey

Thanks for being a part of our community! Before you go:

  • 👏 If you find this helpful Clap for the story and follow the author

(Your support is crucial for me to produce more articles like these, especially since monetization options are available in my country.)

--

--