s2t2
s2t2

Reputation: 2686

Dart how to drop a breakpoint or debugger

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 the dart: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.

Upvotes: 2

Views: 1070

Answers (1)

Miguel Ruivo
Miguel Ruivo

Reputation: 17756

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.

Upvotes: 2

Related Questions