Why does an async function with heavy calculations still freeze the Flutter UI?
Dart is single-threaded. async/await does not spawn threads; CPU-intensive calculations block the main event loop unless dispatched to an Isolate.
Run CPU-heavy operations in parallel background Isolates without freezing the Flutter UI thread
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
One of the most frequent surprises for developers entering Flutter is that async and await do NOT run code on a separate thread.
Dart is single-threaded by default. An asynchronous network call (http.get) or file read does not block the UI because it delegates I/O work to the underlying operating system kernel. However, if you execute a heavy CPU calculation (such as parsing a 20MB JSON array, calculating a hash, or processing an image), that calculation runs directly on the main UI thread, halting animations and causing severe frame drops (jank).
To achieve true multi-core parallel processing in Dart, you must use Isolates.
In traditional languages like Java or C++, threads share memory heaps, requiring locks, mutexes, and synchronized blocks to prevent data race conditions.
In Dart, every thread of execution is called an Isolate:
Isolates communicate exclusively by passing messages (copies of data or transferred memory buffers) back and forth.
Isolate.run()In Dart 2.19 and Dart 3, Google introduced Isolate.run(). It simplifies 90% of concurrency use cases into a single line.
Isolate.run() automatically:
// Heavy parsing function
List<Product> parseLargeJson(String jsonString) {
final List<dynamic> decoded = jsonDecode(jsonString);
return decoded.map((e) => Product.fromJson(e)).toList();
}
// Running off the main UI thread:
Future<List<Product>> loadProductsConcurrently(String jsonString) async {
// Dispatches parseLargeJson to a background isolate:
return await Isolate.run(() => parseLargeJson(jsonString));
}
At 60 FPS, your app has 16.6ms to calculate and paint a frame (at 120 FPS, only 8.3ms). If any synchronous computation takes longer than 10ms, offload it to Isolate.run() immediately.
If you need a continuous background worker (for example, reading high-speed WebSocket audio packets or image filtering), spawning and terminating isolates repeatedly with Isolate.run() has overhead. Instead, set up a persistent isolate with Ports.
1import 'dart:isolate';23// Worker entrypoint running in background isolate4void backgroundWorker(SendPort mainSendPort) {5final workerReceivePort = ReceivePort();6// Send worker's port back to main thread7mainSendPort.send(workerReceivePort.sendPort);89// Listen for tasks from main isolate10workerReceivePort.listen((message) {11if (message is int) {12final result = message * message; // Heavy math13mainSendPort.send(result);14}15});16}1718// Spawning from main isolate19Future<void> startPersistentWorker() async {20final mainReceivePort = ReceivePort();21await Isolate.spawn(backgroundWorker, mainReceivePort.sendPort);2223// Get the worker's SendPort24final workerSendPort = await mainReceivePort.first as SendPort;2526// Send work to background isolate27workerSendPort.send(42);28}
Isolates cannot pass live UI references (like BuildContext, Widget instances, or open file sockets). Only primitive data, data transfer models, and binary buffers can cross isolate boundaries.
Run CPU heavy operations in parallel background Isolates without freezing the Flutter UI thread. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Connect each concept to real app features you can build.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
Dart is single-threaded. async/await does not spawn threads; CPU-intensive calculations block the main event loop unless dispatched to an Isolate.
Dart Isolates do not share memory heaps. Because memory is completely isolated, race conditions and deadlocks are impossible.
Isolate.run() spawns an isolate, runs the closure, returns the result, and terminates the isolate automatically.
BuildContext and widgets belong to the main UI isolate and cannot be transferred across isolate memory boundaries.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue