Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flutter Debugging and DevTools

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

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

)

{

  print

(

'Debug info'

)

;

}

// Print an object (requires toString)

final

user

=

{

'name'

:

'Zhang San'

,

'age'

:

25

}

;

print

(

'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

  1. Click to the left 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

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


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.