简体   繁体   中英

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 ?

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.

启动选项

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM