Sub topic 1
Why Heavy Work Freezes Your App
Prerequisites
This chapter assumes that you know basic Dart syntax, async and await, Futures, and simple streams. For Flutter code, you should also know that the UI must respond quickly to input and draw frames regularly.
The event loop has one worker
Dart runs synchronous code on an event loop. An event loop repeatedly takes a pending task, runs it until it finishes, and then handles the next task.
In a Flutter application, the same isolate usually handles application code and UI-related work. A frame is commonly produced about every 16.7 milliseconds on a 60 Hz display. If synchronous code runs for longer than that, the next frame cannot start on time. The user sees a pause, a dropped frame, or visible jank.
The event loop also handles input, timers, stream events, and completed asynchronous operations. A long calculation delays all of them.
Asynchronous does not mean parallel
async and await help your program wait without blocking while another operation is in progress, such as a file read or network request. They do not automatically move CPU-heavy Dart code to another thread.
For example, this loop still runs synchronously:
import 'dart:async';
void main() {
var frames = 0;
final timer = Timer.periodic(const Duration(milliseconds: 16), (_) {
frames++;
print('frame callback $frames');
if (frames == 3) {
timer.cancel();
}
});
Future<void>.delayed(Duration.zero, () {
print('zero-delay event');
});
final stopwatch = Stopwatch()..start();
var checksum = 0;
while (stopwatch.elapsedMilliseconds < 300) {
checksum = (checksum + 1) & 0x7fffffff;
}
print('calculation finished: $checksum');
}The timer and delayed callback are scheduled before the loop starts, but they cannot run during the loop. The isolate is busy executing the loop. Their callbacks run only after the synchronous calculation returns control to the event loop.
The same problem occurs inside an async function:
Future<int> calculate() async {
await Future<void>.delayed(Duration.zero);
return expensiveCalculation();
}The await gives the event loop a chance to process other work once. expensiveCalculation() still blocks the isolate when it begins.
Why this matters in Flutter
A blocked UI isolate can cause several symptoms:
| Symptom | Immediate cause |
|---|---|
| Dropped frames | Frame work cannot run on time |
| Delayed taps | Input callbacks wait in the event queue |
| Frozen progress indicators | Rebuilds cannot complete |
| Late timers | Timer callbacks cannot start |
| Delayed stream handling | Stream listeners wait for the isolate |
Small calculations usually do not need special treatment. The cost of moving data to another isolate can exceed the benefit for short work. The important question is whether the calculation can occupy the UI isolate long enough to affect interaction.
> callout tip > Measure before moving code. A task that takes a few milliseconds may be simpler and faster on the current isolate. Repeated parsing, image processing, encryption, large sorting operations, and other work that lasts across multiple frames are stronger candidates for isolation.
A practical boundary
An await separates stages of an asynchronous function, but it does not make each stage concurrent. An isolate provides a separate Dart execution context. Later chapters show how to move CPU-heavy work there and receive its result without blocking the UI isolate.
Exercise
Why does adding async to a function not prevent a long for loop inside that function from freezing the UI?
Show answer
The loop is still synchronous and runs on the current isolate. It blocks the event loop until it finishes. async changes how the function returns a Future; it does not move the loop to another isolate.