D
Dart LanguageAdvanced50 XP6 min read

What are Dart Isolates and when should you use them?

TL;DR: Isolates are Dart's concurrency primitive — independent workers with their own memory heap. They communicate via message-passing (SendPort/ReceivePort), not shared memory. Use compute() for simple one-shot background tasks.

Full Answer

Unlike threads in Java/Kotlin which share memory, Dart Isolates have completely separate memory heaps. All communication happens by passing immutable messages (or transferable objects). This eliminates race conditions and the need for locks.

compute() — simplified API

Flutter's compute() function spawns an isolate, runs a top-level function with a single argument, and returns the result. Ideal for JSON parsing, image processing, and other CPU-heavy work.

Isolate.spawn — full control

For long-lived background isolates (e.g., a database worker), use Isolate.spawn with SendPort/ReceivePort for bidirectional communication.

🎯

compute() has overhead of isolate creation (~10-50ms). For tasks under ~1ms, compute() costs more than it saves. Use it for tasks taking >100ms.

Code Examples

dartcompute() for background JSON parsing
Output
compute(): parses JSON on background thread without blocking UI.
Isolate.spawn: prints result of expensiveComputation(42).

Interview Tip

💡

Dart 3 introduces Isolate.run() which is like compute() but supports closures. This simplifies one-shot isolate usage significantly.

#isolates#concurrency#compute#sendport#receiveport