Sub topic 2
Measure Before You Optimize
This chapter assumes you know basic Dart, StatelessWidget, StatefulWidget, setState, and the build method. You also need to know how to run a Flutter app on a device or emulator.
A rebuild means Flutter calls a widget's build method again. Rebuilds are normal. Flutter can rebuild a large part of a screen without causing a visible problem. The goal is not to eliminate every rebuild. The goal is to find rebuilds that happen too often or do expensive work.
What a rebuild tells you
When state changes, Flutter marks the affected element as dirty. An element is Flutter's runtime object that connects a widget configuration to its place in the widget tree. Flutter later calls build for dirty elements.
A rebuild does not always mean that Flutter repaints every pixel. Flutter compares the resulting widget tree and can reuse existing elements and render objects. Painting is a later stage. Measure both stages before deciding what to change.
flowchart TD
A[State or inherited data changes] --> B[Flutter marks elements dirty]
B --> C[build methods run]
C --> D[Widget tree is compared]
D --> E[Layout and paint may run]Use debugPrintRebuildDirtyWidgets
In a debug build, set debugPrintRebuildDirtyWidgets to true. Flutter then prints widgets that rebuild during each frame.
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
void main() {
// Debug builds only: prints the widgets rebuilt in each frame.
debugPrintRebuildDirtyWidgets = true;
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Directionality(
textDirection: TextDirection.ltr,
child: Text('Rebuild test'),
);
}
}Interact with the screen while watching the debug console. Record which widgets rebuild after one action, such as changing a filter or pressing a button. Repeat the action several times. A single rebuild during startup is less important than a large subtree rebuilding on every keystroke.
This flag is useful for a quick first check, but its output can be noisy. It tells you which widgets rebuild, not how much time each rebuild takes.
> callout note > Use this flag only while investigating. Turn it off when the output no longer helps, because a busy console can make your measurements harder to read.
Measure frame time in DevTools
Flutter DevTools gives a broader view. Run the app in profile mode on a real device when you need performance data:
flutter run --profileOpen DevTools and use the Performance view. Start a recording, perform the slow interaction, and stop the recording. Inspect the frame chart for frames that exceed the display budget. At 60 Hz, a frame has about 16 milliseconds to complete. At 120 Hz, it has about 8 milliseconds.
Look for these signals:
- Repeated build activity after one small state change.
- Long spans in the UI thread.
- Layout or paint work that follows the rebuild.
- Jank, which is visible stuttering caused by missed frame deadlines.
- A slow first frame that does not repeat during interaction.
Profile mode is closer to release performance than debug mode. Debug assertions, logging, and diagnostics can distort timings, so do not use debug timing as your final benchmark.
Decide whether a rebuild needs fixing
A rebuild is a candidate for optimization when it is frequent, expensive, or both. A cheap Text widget rebuilding with a counter is usually harmless. A rebuild that parses data, sorts thousands of items, performs synchronous file work, or creates a large layout may need attention.
Measure a baseline before changing code. Write down the interaction, device, build mode, and observed frame time. Then change one thing and repeat the same interaction. If the frame chart does not improve, keep the simpler code.
Exercise
Why should you measure in profile mode instead of judging performance from debug console output alone?
Show answer
Profile mode gives timing closer to a release app, while debug logging and assertions can add overhead and distort frame measurements.