Sub topic 8
Inspecting a Frame in Timeline Events
Assume you can build a basic Flutter app, use flutter run, and understand widget build() methods. You should also have a device or emulator connected and know how to open the DevTools Performance view from a profile-mode run.
The Flutter frames chart tells you which frames are slow. Timeline Events helps explain why. It shows the work recorded during a selected frame, arranged by time and thread.
Select a slow frame
Start the app in profile mode, reproduce the slow interaction, and open the Performance view in DevTools. Profile mode gives timing data closer to a real release build than debug mode.
In the Flutter frames chart, select a frame whose bar exceeds the frame budget. DevTools opens details for that frame. Select the Timeline Events tab to inspect its recorded operations.
A timeline is a time-ordered trace. Each event has a start time and duration. Long events are candidates for investigation, but a long event is not automatically the root cause. It may be the parent of several smaller operations.
Common entries include:
- Widget build work on the UI thread.
- Layout and painting work on the UI thread.
- Rasterization work on the raster thread.
- Dart garbage collection.
- Image decoding or uploading.
- Platform and GPU operations.
Expand parent events and compare their time ranges. Look for work that overlaps the selected slow frame and consumes most of its duration.
Read event hierarchy and duration
The timeline uses nested events to show which operation caused another operation. For example, a frame event may contain build, layout, and paint events. An expensive build method may contain a custom event created by your application.
When you select an event, inspect:
- Its duration.
- The thread or track where it ran.
- Its parent event.
- Its child events.
- Whether it repeats on every frame or only during a specific action.
A repeated event inside every frame often indicates unnecessary rebuilds, layout work, or painting. A one-time event may be initialization, image decoding, or shader compilation.
The UI thread must prepare the frame. The raster thread turns the recorded scene into pixels. If the UI track is long, inspect Dart code, widget building, layout, and painting. If the raster track is long while the UI track is short, inspect effects, large images, clips, shadows, and complex scenes.
> callout note > Timeline Events show where time was recorded, not which change you should make. Confirm the likely cause by changing one thing and measuring the same interaction again.
Add a named event
Your own timeline events make application-specific work easier to recognize. The Timeline.timeSync method records a synchronous operation with a name.
import 'dart:developer';
List<String> parseCatalog(String raw) {
return Timeline.timeSync('parse_catalog', () {
return raw
.split('\n')
.where((line) => line.isNotEmpty)
.toList();
});
}Run the operation during the interaction you are profiling. In Timeline Events, search for or expand parse_catalog. Compare its duration with the selected frame's duration.
Use a named event around a meaningful operation, such as parsing data or preparing a model. Avoid wrapping every small function. Too many events make the trace harder to read.
Connect the event to a fix
If parse_catalog takes most of a slow frame, do not start by changing visual styling. Move expensive computation away from the UI thread, cache results, or parse incrementally. If the event is short but the raster track is long, changing Dart computation will not solve the problem.
Record the original trace first. Then make one targeted change, repeat the same interaction, and compare the selected frame and named event. A successful fix shortens the relevant event and keeps the frame within its budget.
Exercise
A selected frame has a short UI-thread track but a long raster-thread event. Which kind of cause should you investigate first?
Show answer
Investigate rendering work such as large images, clips, shadows, opacity effects, or a complex scene. The evidence points to rasterization rather than Dart code running on the UI thread.