text
stringlengths
1
474
<code_start>import 'dart:developer';
void someFunction(double offset) {
debugger(when: offset > 30);
// ...
}<code_end>
<topic_end>
<topic_start>
Debug app layers using flags
Each layer of the Flutter framework provides a function to dump its
current state or events to the console using the debugPrint property.info Note
All of the following examples were run as macOS native apps on
a MacBook Pro M1. These will differ from any dumps your
development machine prints.lightbulb Tip
Each render object in any tree includes the first five
hexadecimal digits of its hashCode.
This hash serves as a unique identifier for that render object.<topic_end>
<topic_start>
Print the widget tree
To dump the state of the Widgets library,
call the debugDumpApp() function.<topic_end>
<topic_start>Example 4: Call debugDumpApp()
<code_start>import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: AppHome(),
),
);
}
class AppHome extends StatelessWidget {
const AppHome({super.key});
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: TextButton(
onPressed: () {
debugDumpApp();
},
child: const Text('Dump Widget Tree'),
),
),
);
}
}<code_end>
This function recursively calls the toStringDeep() method starting with
the root of the widget tree. It returns a “flattened” tree.Example 4 produces the following widget tree. It includes:Many widgets that don’t appear in your app’s source.
The framework’s widgets’ build functions insert them during the build.The following tree, for example, shows _InkFeatures.
That class implements part of the Material widget.
It doesn’t appear anywhere in the code in Example 4.When the button changes from being pressed to being released,
this invokes the debugDumpApp() function.
It also coincides with the TextButton object calling setState()
and thus marking itself dirty.
This explains why a Flutter marks a specific object as “dirty”.
When you review the widget tree, look for a line that resembles the following:If you write your own widgets, override the
debugFillProperties() method to add information.
Add DiagnosticsProperty objects to the method’s argument
and call the superclass method.
The toString method uses this function to fill in the widget’s description.<topic_end>
<topic_start>
Print the render tree
When debugging a layout issue, the Widgets layer’s tree might lack detail.
The next level of debugging might require a render tree.
To dump the render tree:<topic_end>
<topic_start>Example 5: Call debugDumpRenderTree()
<code_start>import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: AppHome(),
),
);
}
class AppHome extends StatelessWidget {
const AppHome({super.key});
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: TextButton(
onPressed: () {
debugDumpRenderTree();
},
child: const Text('Dump Render Tree'),
),
),
);
}
}<code_end>
When debugging layout issues, look at the size and constraints fields.
The constraints flow down the tree and the sizes flow back up.In the render tree for Example 5:The RenderView, or window size, limits all render objects up to and
including RenderPositionedBox#dc1df render object
to the size of the screen.
This example sets the size to Size(800.0, 600.0)The constraints property of each render object limits the size
of each child. This property takes the BoxConstraints render object as a value.
Starting with the RenderSemanticsAnnotations#fe6b5, the constraint equals
BoxConstraints(w=800.0, h=600.0).The Center widget created the RenderPositionedBox#dc1df render object
under the RenderSemanticsAnnotations#8187b subtree.Each child under this render object has BoxConstraints with both
minimum and maximum values. For example, RenderSemanticsAnnotations#a0a4b
uses BoxConstraints(0.0<=w<=800.0, 0.0<=h<=600.0).All children of the RenderPhysicalShape#8e171 render object use