Conditional Debugging in Dart

How often have you wanted to your program to hit a breakpoint only when it passes a certain condition?

When this situation comes up you most likely end up doing something like this:

if(x > 0){ 
   (BREAKPOINT IDE MARKER HERE) statement;
}

This just adds clutter to your code and you might even end up duplicating a lot of code just for debugging purposes.

However in Dart there is a better and efficient way to do this!

Enter the debugger method. Note that the debugger method does not hook into WebStorm for debugging, it hooks into Observatory's debugging feature and you can debug your Dart application from Observatory.

We can rewrite our previous example of conditional debugging by replacing the entire if block with the following:

 debugger(when: x > 0);

This tells Observatory to break only when x > 0.

Pretty cool!

If you just want to use Observatory for all your debugging (which I recommend a lot) then you can just call debugger() without any arguments!

It's that simple!

Reference to the debugger method: https://api.dartlang.org/135834/dart-developer/debugger.html