Flutter Debugging and DevTools
This section will introduce Flutter's debugging tools and common debugging techniques.
Introduction to Flutter DevTools
Flutter DevTools is a toolset for debugging and analyzing Flutter applications.
Starting DevTools
$ flutter run
After running the application, the terminal will display the DevTools URL, usually http://127.0.0.1:9100
Common Commands
| Command | Description |
|---|---|
| flutter doctor | Check the development environment configuration |
| flutter analyze | Static code analysis |
| flutter test | Running tests |
print and debugPrint
Example: Basic Debug Output
// Basic print
(
'Hello Debug'
)
;
// Output to the console
// debugPrint avoids truncation of long text
debugPrint
(
'This is a very long piece of text that can be output in full without being truncated'
)
;
// Conditional print
if
(
debugMode
)
{
(
'Debug info'
)
;
}
// Print an object (requires toString)
final
user
=
{
'name'
:
'Zhang San'
,
'age'
:
25
}
;
(
'User info: $user'
)
;
// Output: User info: {name: Zhang San, age: 25}
Assertion - assert
Assertions are used to check conditions during development and only take effect in debug mode.
< h2 class="example">Example: Using Assertions
// Simple assertion
assert
(
user
!=
null
,
'User cannot be empty'
)
;
// Function parameter validation
void
setAge
(
int
age
)
{
// Age must be greater than 0
assert
(
age
0
,
'Age must be greater than 0'
)
;
this
.
age
=
age
;
}
// Assertion in UI build (development only)
Widget build
(
BuildContext context
)
{
// Ensure context is not null
assert
(
context
!=
null
)
;
// Ensure there is a parent Scaffold
assert
(
debugCheckHasMaterial
(
context
)
)
;
return
Scaffold
(
..
.
)
;
}
Breakpoint Debugging
Set breakpoints in VS Code or Android Studio for debugging.
VS Code Debugging
- Click to the left of the line number to set a breakpoint
- Press F5 or click the debug button to start debugging
- Use the debug toolbar to control program execution
Common Breakpoint Commands
| Operation | Description |
|---|---|
| Continue / F5 | Continue to the next breakpoint |
| Step Over / F10 | Step over (do not enter functions) |
| Step Into / F11 | Step into (enter functions) |
| Step Out / Shift+F11 | Step out of the current function |
Flutter Inspector
Flutter Inspector is part of DevTools, used to visualize and inspect the Widget tree.
Main Function
- Widget Tree: view the Widget hierarchy
- Layout Explorer: inspect layout constraints
- Performance Overlay: Display performance metrics
Debugging common issues
Example: Debugging UI Not Updating
// Issue: UI not updating
// Cause: setState was not called after modifying the state
// Incorrect example
void
_increment
(
)
{
_counter
++
;
// The state has changed, but the UI will not update!
}
// Correct example
void
_increment
(
)
{
setState
(
(
)
{
_counter
++
;
// You must call setState
}
)
;
}
// Issue: Messy state management
// Suggestion: Use Provider or another state management solution
Common tools for debugging: print for logs, Inspector for examining the Widget tree, Timeline for analyzing performance.