Sub topic 1
How Flutter Produces a Frame
Prerequisites
You should be able to build and run a Flutter app, write StatelessWidget and StatefulWidget classes, and use flutter run. You need a working Flutter SDK with DevTools, plus a physical device or emulator.
How Flutter Produces a Frame
Flutter draws the screen as a series of frames. Each frame needs work on two threads:
- The UI thread runs your Dart code. It calls
build, thenlayout, thenpaint, and it records the result as a layer tree. - The raster thread takes that layer tree and turns it into pixels on the GPU.
The two threads work as a pipeline. While the raster thread draws frame N, the UI thread can prepare frame N+1. A slow step on either thread delays the frame that depends on it.
The Frame Budget and What Jank Means
Each frame must finish within a fixed time, called the frame budget. A 60 Hz display refreshes every 16.7 ms. A 120 Hz display allows 8.3 ms. If work on either thread runs past the budget, Flutter misses the deadline and the previous frame stays on screen. Users see this as jank: stutter, dropped frames, or uneven motion.
Jank is a symptom. The cause is usually work that runs too long on the UI thread, such as an expensive build method, or on the raster thread, such as complex painting or shader compilation.
Why Profiling Needs Profile Mode
Debug mode adds assertions and checks, and it uses a just-in-time compiler. Its timings are slower than a real app, so measurements taken there mislead you. Release mode is the fastest, but it does not allow DevTools to connect. Profile mode compiles ahead of time like release mode, keeps DevTools connected, and gives timings close to release. Use profile mode for all performance work.
Running in Profile Mode and Opening the Performance View
List your connected devices, then launch the app in profile mode on the device you want:
flutter devices
flutter run --profile -d <device-id>Replace <device-id> with an ID from the first command. Open DevTools from the link the terminal prints, or from your IDE, and select the Performance tab.
The Flutter frames chart shows one bar per frame. Each bar's height reflects the time that frame took. Frames that exceed the budget are highlighted, so you can spot jank at a glance. You will use this chart in the next chapter to separate UI-thread work from raster-thread work.
Exercise
A 60 Hz device shows a frame that takes 22 ms to build, lay out, paint, and rasterize. Is this frame jank, and why?
Show answer
Yes. The budget at 60 Hz is 16.7 ms. The frame takes 22 ms, which exceeds the budget, so Flutter misses the deadline and the user sees a stutter.