Sub topic 16
Looking at the three trees in a running app
This chapter assumes you know the roles of widgets, elements, and render objects from the earlier chapters, and that you can run a Flutter app with flutter run. Here you stop reasoning about the trees and read them from a live app. You will use debug dump functions, keys on the terminal, and DevTools.
Which tool shows which tree
Flutter does not print "the widget tree" and "the element tree" separately. The dump for widgets and elements is one dump, because each element holds its widget. The render tree has its own dump.
| Tool | What it shows | Where it works |
|---|---|---|
| `debugDumpApp()` | Elements, each with its widget, key, state, and render object | Debug builds |
| `debugDumpRenderTree()` | Render objects with constraints, size, and parent data | Debug builds |
| `debugDumpLayerTree()` | The layer tree used for compositing | Debug builds |
| `flutter run` keys `w`, `t`, `L` | The same three dumps, triggered from the terminal | Debug builds |
| DevTools Flutter Inspector | The widget tree, plus details and the Layout Explorer | Debug and profile builds |
Dumping the trees from code
Call the dump functions after the first frame, when all three trees exist. The addPostFrameCallback call below runs after that frame.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(const GreetingApp());
WidgetsBinding.instance.addPostFrameCallback((_) {
debugDumpApp(); // widget and element tree
debugDumpRenderTree(); // render object tree
});
}
class GreetingApp extends StatelessWidget {
const GreetingApp({super.key});
@override
Widget build(BuildContext context) {
return const Directionality(
textDirection: TextDirection.ltr,
child: Padding(
padding: EdgeInsets.all(16),
child: Text('Hello'),
),
);
}
}The output goes to the console. Avoid calling the dumps from inside build. The trees may be mid-update there, and the output will mislead you.
Reading the widget and element dump
The dump is indented so that each line is a child of the line above it. This simplified excerpt keeps only the lines that matter here.
GreetingApp
└Directionality(textDirection: ltr)
└Padding(padding: EdgeInsets.all(16.0), renderObject: RenderPadding#d3e4f)
└Text("Hello")
└RichText(renderObject: RenderParagraph#5a6b7)Three things stand out:
- Only
PaddingandRichTextshow arenderObject.GreetingApp,Directionality, andTextonly compose other widgets or provide data. Textis not the widget that draws. It builds aRichText, andRichTextcreates theRenderParagraph.- Lines for a
StatefulWidgetalso list astate:entry with theStateobject. Elements that need a rebuild are markedDIRTY.
Keys appear in square brackets after the widget name. In the SwapDemo example from the keys chapter, a tile with ValueKey('A') shows as Tile-[<'A'>]. A dump taken before and after the swap shows that the element for A keeps its State and moves to a new position.
Reading the render tree dump
RenderView#a1b2c
└─child: RenderPadding#d3e4f
│ constraints: BoxConstraints(w=800.0, h=600.0)
│ size: Size(800.0, 600.0)
│ padding: EdgeInsets.all(16.0)
└─child: RenderParagraph#5a6b7
parentData: offset=Offset(16.0, 16.0)
size: Size(...)
text: TextSpan(...)The real dump has more properties and the sizes depend on your device. The excerpt shows the layout rule from earlier chapters:
- The root's constraints are tight, so
RenderPaddingfills the screen. RenderPaddingshrinks the constraints by 16 on each side and passes them down.- The paragraph picks a size inside those constraints.
- The padding sets the paragraph's position in
parentData, hereoffset=Offset(16.0, 16.0).
You can check any layout bug against these lines. If a widget has the wrong size, read its constraints: line first. The constraints it received decide what sizes are possible.
Dumping from the terminal
When flutter run is attached to a debug app, you can press a key instead of editing code:
wdumps the widget and element tree.tdumps the render tree.Ldumps the layer tree.Sdumps the semantics traversal order.Udumps the inverse hit-test order.itoggles the widget inspector.
This is the fastest way to compare the trees before and after an interaction. Tap a counter, press w, and look for the DIRTY marker or a changed state: value.
Using the DevTools Flutter Inspector
The Inspector shows the widget tree and lets you select a widget in the running app. Three features are useful for this chapter:
- Summary tree and full tree. By default the tree shows only widgets created by your own code. Turn off the summary view to see framework widgets such as
RichTextandDirectionality. - Details tree. Selecting a widget shows its properties and, for widgets that create one, its render object with constraints and size.
- Layout Explorer. For
Row,Column, andFlexwidgets, it shows the constraints and the flex factors that produced each child's size.
The Inspector uses the widget tree as its starting point, so it does not show render objects as a separate tree. Use debugDumpRenderTree() or the t key when you need that view.
Debug flags that make rebuilds visible
Dumps are snapshots. These flags report work as it happens. Set them in main before runApp.
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
void main() {
debugPrintRebuildDirtyWidgets = true; // log every element rebuilt each frame
debugPrintLayouts = true; // log layout of render objects
debugRepaintRainbowEnabled = true; // tint layers when they repaint
runApp(const GreetingApp());
}debugPrintRebuildDirtyWidgets is useful for checking that setState rebuilds only the subtree you expect. debugRepaintRainbowEnabled draws a changing colored border on each repaint boundary, so a layer that flickers on every frame is repainting every frame.
Common mistakes when inspecting
- Dumping too early. Calling a dump before the first frame can fail or print an empty tree. Use a post-frame callback.
- Looking for a render object on every widget. Most widgets do not have one, as the diagram shows. A missing
renderObjectis normal forStatelessWidget,StatefulWidget, andInheritedWidgetelements. - Expecting a widget tree to persist. Widget objects are recreated on every rebuild. Compare element and render object identities, shown after
#in the dump, to see what was reused. Hash values that stay the same across dumps mean the object was kept. - Reading the Inspector as the render tree. It starts from widgets. For constraints and sizes, use the Details tree or the render dump.
Exercise
You add a Tile with ValueKey('A') to a list and reverse the list. Before and after the reversal you press w in the terminal. What should you compare in the two dumps to confirm that the key preserved state?
Show answer
Compare the state: entries (the _TileState#... hash) for the tile keyed A. If the hash is the same in both dumps, the same State object moved with the key. The tile's position in the tree should have changed, and its tap count should be unchanged.
Exercise
A dump shows Text("Hello") with no renderObject entry, but RichText below it has renderObject: RenderParagraph#.... Why, and which widget would you look at in the Inspector to see the text's size?
Show answer
Text is a StatelessWidget that composes other widgets, so its element has no render object. It builds RichText, which is a render object widget and creates the RenderParagraph. To see the size, select the RichText (turn off the summary tree if needed) and read its render object details, or read the size: line of RenderParagraph in the debugDumpRenderTree() output.