Advanced
Running Heavy Work in the Background in Flutter with Isolates
· 9 min read
Why Is Dart Single-Threaded, and What's the Problem?
Dart code runs by default inside a single isolate on an event loop. The UI thread lives in that same isolate: every animation frame, every gesture, every build call is processed in order on that one thread. This is a simple and safe model — you don't deal with locks or race conditions — but it has a downside: if you run a long, synchronous task on the event loop, no frame can be drawn and no touch can be processed for that whole duration. The user interface freezes.
A network request made with await doesn't cause this problem, because the I/O wait time doesn't block the event loop. But a synchronous, CPU-heavy task — decoding a large JSON, sorting a huge list, processing an image — genuinely blocks the event loop.
The Simple Fix: compute()
Flutter's compute() function runs a top-level or static function you give it in a separate isolate in the background, returns the result as a Future, and automatically shuts the isolate down once the work is done. It's the simplest route for one-off, pure work that doesn't touch outside state:
List<Product> _parseProducts(String jsonString) {
final List<dynamic> decoded = jsonDecode(jsonString) as List<dynamic>;
return decoded
.map((item) => Product.fromJson(item as Map<String, dynamic>))
.toList();
}
Future<List<Product>> loadProducts(String jsonString) {
return compute(_parseProducts, jsonString);
}compute() sends the function and a single parameter to another isolate. Because of this, the function must be top-level (outside a class) or static, and both the parameter and the result must be types that can be sent between isolates (serializable as messages).
A Manual Isolate: Isolate.spawn
compute() opens a new isolate on every call and closes it once the work is done; it isn't a good fit for a long-lived background job that exchanges messages continuously. For that, you spawn the isolate yourself with Isolate.spawn and send/receive as many messages as you want through a SendPort/ReceivePort pair:
void _workerEntryPoint(SendPort mainSendPort) {
final workerReceivePort = ReceivePort();
mainSendPort.send(workerReceivePort.sendPort);
workerReceivePort.listen((message) {
if (message is int) {
final result = _heavyComputation(message);
mainSendPort.send(result);
}
});
}
Future<SendPort> startWorker(SendPort mainSendPort) async {
final readyPort = ReceivePort();
await Isolate.spawn(_workerEntryPoint, readyPort.sendPort);
return await readyPort.first as SendPort;
}Here, the main isolate first opens a ReceivePort and hands the worker it starts with Isolate.spawn its own SendPort; the worker then creates its own ReceivePort and sends its SendPort back to the main isolate. Once this mutual port exchange is done, the two isolates can exchange as many messages as they like.
Isolates Don't Share Memory
Each isolate has its own memory and its own event loop. Two isolates can't share an object directly; they can only send messages. A sent message must be either a primitive type (int, String, bool, List, Map, and so on) or a specially transferable type like TransferableTypedData. This restriction rules out bugs like data races at the root, but it also means you need to keep in mind the cost of copying a large shared object on every message.
Which One Should You Pick?
• A one-off, pure computation (parsing, sorting, encoding/decoding): compute(). • A long-lived background job with a continuous stream of data, such as listening to a socket connection or running periodic work: a manual worker with Isolate.spawn. • I/O-heavy work (network requests, reading files): no isolate needed, since await already doesn't block the event loop.
Conclusion
One of the most common causes of jank in Flutter is a synchronous, heavy task blocking the event loop. compute() makes a huge difference with very little code for one-off, pure work. If you need a long-lived background job with continuous messaging, move to Isolate.spawn — but remember that isolates don't share memory, only messages pass between them.