Jack
Jack

Reputation: 16718

Debug current open Flutter file in VSCode

I am writing a Flutter tutorial project in VS Code with multiple main() functions.

For example:

main.dart  //Contains a main() function
step1.dart //Contains a main() function
step2.dart //Contains a main() function

If I have step1.dart open, then pressing F5 runs main.dart and not step1.dart in debug mode.

I can hover the mouse over the main() function in step1.dart, and then select 'debug' from the context menu. This works as expected and runs step1.dart in debug. However, there is no associated shortcut.

What can I press to run the active open file in debug mode, not main.dart?

Upvotes: 2

Views: 1717

Answers (2)

Satoshi
Satoshi

Reputation: 71

I made this launch.json file in the .vscode folder. Worked like a charm.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Dart: Current File",
      "type": "dart",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}"
    },
  ]
}

Here are the variables you can use in launch.json.

https://code.visualstudio.com/docs/editor/variables-reference

Upvotes: 2

mondayrris
mondayrris

Reputation: 724

As long as I have experience of having multiple flutter projects on a workspace (not too good feedback at all), the key is to set the pre-build process configuration file .vscode/launch.json, you can get more info on debugging#_launch-configurations.

When you create the file, it will look something like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flutter",
            "request": "launch",
            "type": "dart",
        }
    ]
}

Then add "program": "lib/your-entry-point.dart":

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "main",
            "request": "launch",
            "type": "dart",
            "program": "lib/main.dart"
        },{
            "name": "step1",
            "request": "launch",
            "type": "dart",
            "program": "lib/step1.dart"
        },{
            "name": "step2",
            "request": "launch",
            "type": "dart",
            "program": "lib/step2.dart"
        }
    ]
}

This will then create the following launch options.

Launch options

Upvotes: 4

Related Questions