Dart how to drop a breakpoint or debugger
You can't debug without a debugger attached, that's why running from the command line will hang in the breakpoint, since you can step on the next instruction.
If you use VS Code or Intellij and use the debugger()
while in debug mode Shift+Cmd+R, it will trigger a breakpoint there and you can analyze your variables and step forward to the next instructions.
s2t2
Updated on December 13, 2022Comments
-
s2t2 over 1 year
In Python, I can insert a
breakpoint()
keyword on any line of code, and when I run the script from the command-line, it will stop when it reaches that line, and I'll have an opportunity to interact with or access any previously-defined variables. I was looking for a way to do this in the Dart language but so far without success.I've seen references to the
debugger
keyword provided by thedart:developer
library, but instead of allowing me to interact, the script just hangs:// bin/my_script.dart import "dart:developer"; // source of debugger(); main() { var x = 5; print("X: ${x}"); //> X: 5 debugger(); // ... just hangs print("END"); }
I've also seen references to the
console
package, but I'm not seeing it do anything:// bin/my_script.dart import "package:console/console.dart"; // source of Console.init() main() { var x = 5; print("X: ${x}"); //> X: 5 Console.init(); // ... nothing happens print("END"); }
FYI: I'm running this script via
dart bin/my_script.dart
, and a command-line solution would be ideal, but a solution using the VS Code text editor would also be sufficient. -
s2t2 almost 5 yearsShift Command R does bring up an opportunity for me to "play", but when I press play, it plays right through the
debugger()
without stopping, and I don't see any output from my script at all. -
Miguel Ruivo almost 5 yearsThere must be something wrong. I've just tried and when I use
debugger()
while in debug mode (play button) it stops there and let me step over the next instructions. -
s2t2 almost 5 yearsIt does seem to do something when the script is
lib/main.dart
but not differently-named files. When I do it with thelib/main.dart
script, it does stop, and I see information in the left pane, but how do I find the console where I can type variable names and statements / interpret dart expressions? -
Miguel Ruivo almost 5 yearsYou can write expressions directly on the debug console itself (View > Debug console).