Sub topic 6
Pausing Code with Breakpoints and Stepping
Assume you know basic Dart syntax, asynchronous programming, Flutter widgets, and the build method. This chapter focuses on tracing application logic with the Dart debugger in Flutter DevTools.
When an app behaves incorrectly, logs can show what happened, but a debugger lets you pause execution and inspect what is happening at one exact line. You can view variables, follow the call stack, and execute the program one statement at a time.
Set a breakpoint
A breakpoint is a marker that pauses a running program when execution reaches a selected line. Add one by clicking the gutter beside a line in your IDE, or by selecting a line in the Debugger view in DevTools.
Start the app in debug mode:
flutter runThen open DevTools from your IDE or run it separately:
dart devtoolsConnect DevTools to the Dart VM service URI shown by flutter run. The Debugger view displays the source files and active breakpoints.
When execution pauses, the app stops at the breakpoint. The Debugger view usually shows:
- Local variables and their current values.
- The call stack, which lists the functions that called the current function.
- The current source line.
- Controls for continuing, stepping over, stepping into, and stepping out.
A breakpoint is useful when you know roughly where the wrong value is produced. Place it before the suspected calculation, not only after the final error.
Use conditional breakpoints
A conditional breakpoint pauses only when its condition evaluates to true. This is useful inside a loop or for a problem that occurs only with particular data.
For example, a breakpoint in a list-processing loop could use this condition:
item.id == 42The exact breakpoint interface depends on your IDE, but the condition is evaluated by the Dart debugger. Keep conditions simple. A condition that calls code or changes state can make debugging confusing.
You can also configure DevTools to pause when an exception is thrown. This helps when an exception is caught before it reaches the Flutter error screen. Exception pause settings are available in the Debugger view.
Step through execution
After the debugger pauses, use the stepping controls to follow the program:
- Continue resumes execution until the next breakpoint or exception.
- Step over runs the current line without entering a function called by that line.
- Step into enters the called function so you can inspect its implementation.
- Step out finishes the current function and pauses where it returns to its caller.
The call stack is especially useful when a callback, event handler, or state-management method produces an unexpected result. Select an earlier stack frame to inspect that caller's variables and source line.
Asynchronous code can make the stack change between pauses. An await suspends the current function, allowing other work to run. After the awaited operation completes, execution may resume with a different stack. Inspect the values both before and after the await.
Pause only for an unexpected result
The debugger function from dart:developer creates a programmatic breakpoint. Its when argument makes the pause conditional.
import 'dart:developer';
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: DiscountPage()));
}
double applyDiscount(double total, double discount) {
final result = total - discount;
// The debugger pauses here only when the result is invalid.
debugger(when: result < 0, message: 'Negative total');
return result;
}
class DiscountPage extends StatelessWidget {
const DiscountPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Discount test')),
body: Center(
child: ElevatedButton(
onPressed: () {
final total = applyDiscount(10, 25);
debugPrint('Final total: $total');
},
child: const Text('Apply discount'),
),
),
);
}
}Run this app in debug mode with DevTools connected, then press the button. The debugger pauses because result < 0 is true. Inspect total, discount, and result before deciding whether the caller or the calculation is wrong.
A breakpoint investigation
A practical investigation is short and deliberate:
- Reproduce the problem in debug mode.
- Set a breakpoint before the suspicious operation.
- Continue until the breakpoint pauses execution.
- Inspect inputs, local variables, and the call stack.
- Step into a function only when its implementation is relevant.
- Change the code, restart or hot reload as appropriate, and reproduce the same case.
- Remove temporary breakpoints after verifying the fix.
Do not treat a paused line as proof that the line is faulty. The incorrect value may have entered through a caller, a parsed response, or an earlier state update.
Exercise
A button handler calls applyDiscount(10, 25), and the app produces a negative total. Where should you place a conditional breakpoint or debugger call, and what should you inspect first?
Show answer
Pause after the calculation of result, when result < 0 is true. Inspect total, discount, the call stack, and the caller's inputs before changing the calculation.