Sub topic 13
A Repeatable Rebuild Audit Workflow
This chapter assumes you know basic Dart, StatelessWidget, StatefulWidget, setState, and the build method. You should also be able to run a Flutter app on a device or emulator.
A rebuild is a new call to a widget's build method. Flutter may rebuild a widget because its State called setState, an ancestor changed, or an inherited dependency changed. A rebuild does not automatically mean Flutter repaints pixels or performs expensive layout. First measure the actual cost.
Start with a user-visible symptom
Do not optimize every rebuild. A build method can run often and still be cheap. Begin with a symptom such as:
- A tap causes visible delay.
- Scrolling drops frames.
- Opening the keyboard makes the screen stutter.
- A large list responds slowly to updates.
- Animation performance falls below the device's refresh rate.
Write down the action that causes the problem and the part of the screen that appears involved. This gives you a repeatable test case.
Measure rebuilds in a debug build
Use debugPrintRebuildDirtyWidgets for a quick first look. It logs widgets that rebuild during each frame.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
// Use this only while investigating a debug build.
debugPrintRebuildDirtyWidgets = true;
runApp(
const MaterialApp(
home: Scaffold(
body: Center(child: Text('Audit demo')),
),
),
);
}Reproduce the action several times and look for patterns. One tap that rebuilds a small counter is usually harmless. One tap that rebuilds an entire product page, including unrelated headers and list items, deserves investigation.
The log is evidence about rebuild scope, not frame performance. It can also be noisy. Turn it off after the initial inspection.
Measure frame time with DevTools
Use Flutter DevTools to inspect the Performance view. Run the app in profile mode on a physical or representative device:
flutter run --profileStart a performance recording, perform the slow action, stop the recording, and inspect the relevant frames. Look at UI-thread time, raster-thread time, and frame duration. A frame that exceeds the device's frame budget may produce jank.
Profile mode is important because debug mode adds checks and logging that distort timings. Use the same data size and interaction each time. Compare a baseline recording with a recording after one change.
Form one hypothesis
Turn the evidence into a specific statement:
- “Changing the filter rebuilds the whole page because the filter state belongs to the page state.”
- “The name label rebuilds when unrelated user fields change.”
- “Every list item rebuilds when one item changes.”
- “The build method performs sorting on every frame.”
Then make one small change that tests that statement. Avoid changing state placement, list construction, inherited lookups, and painting boundaries at the same time. You need to know which change affected the result.
Use a narrow fix
Common fixes include:
- Move frequently changing state into a small
StatefulWidget. - Add
constconstructors and useconstfor unchanged subtrees. - Extract a widget class instead of returning a large subtree from a helper method.
- Pass static content through a builder's
childparameter. - Use
ValueListenableBuilderor a selector to listen to only the required value. - Replace broad lookups such as
MediaQuery.of(context)with a narrow accessor such asMediaQuery.sizeOf(context). - Use
ListView.builderfor long lists and stable keys for item identity.
A widget class gives Flutter a separate element boundary for the extracted subtree. A helper method only organizes Dart code; it does not create a separate widget boundary.
Verify the change
Repeat the exact baseline interaction. Check both kinds of evidence:
| Evidence | What it tells you |
|---|---|
| Rebuild log | Which elements rebuild and whether scope changed |
| DevTools frame data | Whether frame time and thread work improved |
| Visual behavior | Whether the screen still updates correctly |
If rebuild scope became smaller but frame time did not improve, the rebuilds may have been cheap. Keep the simpler code unless the narrower scope improves future maintenance. If frame time improved, record the reason and keep the profiling scenario for regression testing.
Audit flow
Exercise
Exercise
A button changes one counter, but the debug log shows that the entire screen rebuilds. What should you do first?
Show answer
Measure the interaction in profile mode, then inspect whether the counter state can move into a smaller widget. Apply one change and compare the same interaction with the baseline.