AdvancedAdvanced Flutter

What are isolates in Dart and when do you need them?

Short Answer

An isolate is Dart's unit of concurrency — a separate memory heap and event loop running independently, with no shared mutable state — used to run CPU-heavy work in parallel without blocking the UI thread.

Dart's main isolate runs your UI and event loop; `await`ing a network call doesn't block it because I/O is non-blocking, but genuinely CPU-bound work (parsing a huge JSON file, image processing, heavy computation) does block it, causing dropped frames and jank, because there's no thread for it to yield to.

Isolates solve this by running that work on a separate isolate with its own memory — communication happens only via message-passing (ports), never shared memory, which is why Dart doesn't need locks/mutexes for isolate-to-isolate communication. The easy way to spin one up for a single task is `compute()` from Flutter, which runs a top-level function on a new isolate and returns its result as a Future. For longer-lived background work, you manage an `Isolate` and `ReceivePort` directly.

Code Example

// Runs on a separate isolate, doesn't block the UI thread
final result = await compute(parseHeavyJson, rawJsonString);

List<Item> parseHeavyJson(String raw) {
  final data = jsonDecode(raw) as List;
  return data.map((e) => Item.fromJson(e)).toList();
}

Common Mistakes

  • ×Using isolates for I/O-bound work (network calls) that already doesn't block the main isolate — isolates only help with CPU-bound work.
  • ×Passing non-message-safe objects (like a class holding a `BuildContext`) across the isolate boundary.

Interview Tips

  • Mention `compute()` by name — it's the practical, most-used entry point and shows real usage, not just textbook knowledge.