الانتقال إلى المحتوى
تطبيقات الموبايلDart· 7 دقائق للقراءة

async Isn't a Background Thread: Dart's Event Loop and When You Need an Isolate

async lets Dart wait without blocking, but your code still runs on one isolate: heavy work needs another one.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Dart 3
محتويات المقال

A shop app loads its catalog as one big JSON response. The developer made every step async, and still, each time the list arrives, the loading spinner freezes for a moment and a scroll in progress stutters. The review comment on the fix reads "it can't be the parsing, it's already async". It is the parsing. async was never going to move it anywhere.

The common misconception

The belief: marking a function async sends its work to the background. As long as the heavy code sits inside an async function and the caller awaits it, the UI stays free.

It's a reasonable guess. In languages such as C# or Kotlin, asynchronous code often does end up on other threads, through a thread pool or a dispatcher. And in Dart, part of it really is true: while a network request or a file read is in flight, your app keeps drawing frames and handling taps. So async looks like "runs in the background". The waiting does happen elsewhere. Your Dart code doesn't.

What actually happens

One isolate, one thread, one event loop

Dart code runs inside an . The Dart docs describe it as being like a thread or a process, except that each isolate has its own memory and a single thread running an event loop. By default, a Flutter app does all of its work on one of them, the main isolate.

The event loop takes events from a queue and processes them one at a time, in order: a tap, a timer, a response that arrived, the rest of an async function. Flutter adds its own events to that queue: on a 60 Hz device, a "paint frame" event 60 times per second. If one event takes too long, the frame events behind it wait, and the user sees .

An async function runs synchronously until its first await

async doesn't start anything new. The Dart docs put it plainly: an async function runs synchronously until the first await. Run this:

Dart order.dart
Future<void> work() async {  print('2: work starts, synchronously');  await Future<void>.delayed(Duration.zero);  print('4: after the await, same isolate');}void main() {  print('1: main');  work();  print('3: main continues');}

It prints 1, 2, 3, 4. Calling work() runs its first line right away, on the caller's stack. The await suspends the function and returns a Future to the caller, which carries on. When the awaited future completes, the rest of work() is scheduled to run later, on the same event loop. At no point does a second thread appear.

await waits; it doesn't compute

When you await an HTTP call or a file read, the platform does the actual I/O outside your Dart code. Meanwhile the event loop is free, so frames and taps keep being processed. That's the half of the belief that holds.

Once the response arrives, though, whatever you do with it is ordinary synchronous Dart: jsonDecode, sorting 50,000 items, filtering a big list, resizing an image. It runs on the event loop, and while it runs, nothing else does. No frame, no tap handler, no timer. Wrapping it in Future(() => work()) doesn't help either: that constructor runs the computation later through Timer.run, on the same isolate. It changes when the work runs, not where.

Isolates: a second event loop with its own memory

Real parallelism in Dart means a second isolate. Isolates share no memory and talk only through messages, so values you send are copied (immutable ones like strings are shared). Isolate.run does the whole round trip for you: it spawns a worker isolate, runs your function there, returns the result to the main isolate and shuts the worker down. The result comes back through Isolate.exit, which hands it over without copying it.

Here's the catalog loader, both ways:

Dart catalog_loader.dart
import 'dart:convert';Future<List<Object?>> loadCatalog(Future<String> Function() fetch) async {  final body = await fetch(); // I/O wait: frames keep coming  // Still the main isolate: no frame until the decode returns  return jsonDecode(body) as List<Object?>;}

Measure it and the difference is plain. With a generated 15.8 MB response of 200,000 products, the first version decoded in about 190 ms, and a 16 ms timer standing in for frames didn't fire once in that time. That's more than ten frames at 60 Hz. The Future(() => …) variant behaved the same. With Isolate.run, the whole call took longer, about 290 to 340 ms including starting the worker, but the timer kept firing as often as it did with the app idle.

That's the honest trade-off: an isolate doesn't make work cheaper. It costs a spawn and a copy of what you send, and in return the main isolate keeps producing frames.

MythReality
async runs the function on a background threadIt runs on the caller's isolate, synchronously until the first await
await keeps the UI responsiveOnly while waiting; the code after it blocks frames like any other code
Future(() => work()) moves work off the UIIt queues the work for later on the same isolate
Isolates share memory like threadsEach has its own memory; messages are copied, results transferred
Isolate.run makes the work fasterIt adds overhead; it keeps frames coming, it doesn't shrink the work

How to apply it in your project

  1. Separate waiting from computingRead your async functions line by line. Each await on I/O is waiting, which is cheap. Each decode, loop, sort or image operation after it is computing, which blocks the main isolate for as long as it takes.
  2. Measure before you move anythingRun the app in profile mode and open the DevTools Performance view, which marks frames slower than about 16 ms (on a 60 Hz screen) with a red overlay. Look at which Dart code ran in those frames. Small payloads rarely need an isolate.
  3. Move the heavy, pure part with Isolate.runPass plain data in (a string, a list, bytes) and return plain data out. Keep the closure small, or call a top-level function, so it doesn't drag this along.
  4. Keep UI work on the main isolateWidgets, rootBundle assets and all other UI work are tied to the main isolate. Load the asset there, then send its contents to the worker. For the same job repeated many times, a long-lived isolate from Isolate.spawn with ports avoids paying the spawn cost on every call.

Key takeaways

  • async doesn't create a thread: the function runs on the caller's isolate until its first await.
  • await frees the event loop only while waiting; the code after it runs on the same isolate.
  • CPU-heavy work on the main isolate delays frames and taps, async or not.
  • Isolate.run moves work to a worker with its own memory, at the cost of a spawn and a copy.
  • Send plain data to isolates, keep UI work on the main one, and use compute when you also target the web.

📚 المصادر

  1. Concurrency in Dart — Dart
  2. Asynchronous programming: futures, async, await — Dart
  3. Isolates — Dart
  4. Concurrency and isolates — Flutter docs
  5. Isolate.run — Dart API

التعليقات (0)