Sub topic 10
Enhancing Flutter Traces with Build, Layout, and Paint Tracking
This chapter assumes that you can run a Flutter app, understand widget build methods, and use the DevTools Performance view in profile mode. The goal is to make a slow frame easier to explain by adding details about widget builds, layout, and painting.
Why the default trace may be insufficient
Flutter records important frame information by default. You can usually see whether the UI thread or raster thread took too long. However, the trace may not immediately show which widget caused the work.
Enhanced tracing adds more detailed timeline events:
- Build tracking records widget build methods.
- Layout tracking records render objects performing layout.
- Paint tracking records render objects painting their contents.
These events help connect a long frame to a part of your widget tree. They also add tracing overhead, so enable them while investigating a problem rather than leaving them on for every performance run.
The relationship looks like this:
Enable tracing in DevTools
Start the app in profile mode and connect DevTools to that process. In the Performance view, open the tracing options and enable the build, layout, or paint details. The exact labels can differ between DevTools versions, but they are grouped under enhanced tracing options.
Use the smallest useful set. For example, enable build tracking when many widgets rebuild. Enable layout tracking when changing one widget appears to move or resize many others. Enable paint tracking when the UI thread looks acceptable but rendering a frame takes too long.
Then reproduce the problem. Select a new slow frame and inspect its timeline. Existing frames do not gain details retroactively.
Enable the same options from code
You can turn on the framework flags before runApp. This is useful when reproducing a problem requires a particular startup sequence or automated test.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() {
// Adds timeline events for each widget build, layout, and paint.
// Use these flags for profile investigations, not normal release runs.
debugProfileBuildsEnabled = true;
debugProfileLayoutsEnabled = true;
debugProfilePaintsEnabled = true;
runApp(
const MaterialApp(
home: Scaffold(
body: Center(child: Text('Tracing enabled')),
),
),
);
}These flags do not fix a slow frame. They make the recorded evidence more specific. After changing the code, restart the profile run so the flags are applied before the first frame.
Interpret the added events
A long build event suggests that Dart code is doing too much work while widgets rebuild. Common causes include filtering a large list in build, decoding data synchronously, or rebuilding a large subtree after a small state change.
A long layout event suggests that constraints or geometry are expensive to calculate. Deeply nested layouts, large lists of newly created children, and repeated intrinsic measurement are common examples.
A long paint event suggests that Flutter is spending too much time creating drawing commands. Large clips, opacity layers, shadows, and complex custom painting can contribute.
Do not treat the number of events as the main result. Look for events whose duration overlaps the slow frame and explains the thread that exceeded its budget.
Use the evidence to choose a fix
Tracing should narrow the investigation:
- Select a slow frame in the Performance view.
- Identify whether build, layout, or paint work dominates.
- Enable the matching enhanced trace category.
- Find the longest relevant event.
- Change only the code connected to that event.
- Disable extra tracing and measure again in the same profile scenario.
For example, if one parent rebuilds thousands of list rows, reducing the rebuild scope is more targeted than changing painting behavior. If paint events dominate, reducing visual effects may help more than changing widget state management.
Exercise
A frame has a long UI-thread duration, and enhanced tracing shows one parent widget building a large subtree. Which investigation should come first: reducing shadows or reducing the rebuild scope?
Show answer
Reduce the rebuild scope first. The trace identifies widget build work as the likely cause, so the first change should limit how much of the subtree rebuilds.