Flutter Debugging and DevTools

This section will introduce Flutter's debugging tools and common debugging techniques.


Introduction to Flutter DevTools

Flutter DevTools is a suite of tools for debugging and analyzing Flutter applications.

Launch DevTools

$ flutter run

After running the application, the terminal will display the DevTools URL, usuallyhttp://127.0.0.1:9100

Common Commands

CommandDescription
flutter doctorCheck development environment configuration
flutter analyzeStatic Code Analysis
flutter testRun tests

print and debugPrint

Example: Basic Debug Output

// Basic printing
print('Hello Debug');  // Output to console

// debugPrint avoids long text truncation
debugPrint('This is a long text that can be output in full without being truncated');

// Conditional printing
if (debugMode) {
  print('Debug info');
}

// Print object (requires toString)
final user = {'name': 'Zhang San', 'age': 25};
print('User info: $user');  // Output: User info: {name: John Doe, 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;
}

// Assertions in UI building (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

  1. Click on the left side of the line number to set a breakpoint
  2. Press F5 or click the debug button to start debugging
  3. Use the debug toolbar to control program execution

Common Breakpoint Commands

OperationDescription
Continue / F5Continue execution to the next breakpoint
Step Over / F10Step over, without entering functions
Step Into / F11Step into, entering functions
Step Out / Shift+F11Step Out of Current Function

Flutter Inspector

Flutter Inspector is part of DevTools, used to visualize and inspect the Widget tree.

Main Features

  • Widget Tree: View the Widget hierarchy structure
  • Layout Explorer: Check layout constraints
  • Performance Overlay: Display performance metrics

Debugging Common Issues

Example: Debugging UI Not Updating

// Issue: UI not updating
// Reason: setState was not called after modifying the state

// Wrong example
void _increment() {
  _counter++;  // State has changed, but the UI won't update!
}

// Correct example
void _increment() {
  setState(() {
    _counter++;  // Must call setState
  });
}

// Issue: state management is messy
// Suggestion: Use Provider or other state management solution

Common tools for debugging: print for outputting logs, Inspector for inspecting the Widget tree, and Timeline for analyzing performance.

other extensions