You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart新手求助:VSCode中如何实现保存代码时自动运行程序

Auto-Restart Dart Programs on Save in VSCode (Like Nodemon)

Hey there! Great question—VSCode absolutely supports auto-restarting your Dart scripts when you save files, just like nodemon does for Node.js. Since you're already set up with Dart, Flutter, and VSCode, here are the most straightforward ways to get this working:

Dart 2.14+ comes with a native watch mode that automatically restarts your program when files change—no extra tools needed:

  • Via Terminal: Open VSCode's integrated terminal and run:
    dart run --watch your_main_file.dart
    
    From now on, every time you save a .dart file, your program will restart instantly.
  • Via Debug Panel: If you prefer launching from the debug sidebar, edit your .vscode/launch.json file to add this configuration:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Dart (Auto-Restart)",
                "type": "dart",
                "request": "launch",
                "program": "your_main_file.dart",
                "args": ["--watch"]
            }
        ]
    }
    
    Select this new configuration in the debug panel, hit play, and you’re good to go.

Option 2: VSCode Tasks + File Watcher

For more control over the workflow, you can use VSCode’s task system paired with a file-watching extension:

  1. Open the command palette (Ctrl+Shift+P / Cmd+Shift+P), search for Tasks: Configure Task, then choose Create tasks.json file from template → Others.
  2. Replace the default tasks.json content with this:
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Run Dart Auto-Restart",
                "type": "shell",
                "command": "dart run your_main_file.dart",
                "problemMatcher": [],
                "isBackground": true,
                "presentation": {
                    "panel": "shared",
                    "reveal": "always"
                }
            }
        ]
    }
    
  3. Install the File Watcher extension for VSCode, then configure it to trigger the "Run Dart Auto-Restart" task whenever a .dart file is saved.

Option 3: Use Nodemon Directly

Since you’re familiar with nodemon from Node.js, you can repurpose it for Dart too:

  1. Make sure nodemon is installed globally: npm install -g nodemon
  2. Create a nodemon.json file in your project root:
    {
        "watch": ["*.dart"],
        "exec": "dart run your_main_file.dart",
        "ext": "dart"
    }
    
  3. Run nodemon in VSCode’s terminal, and it’ll restart your Dart program every time you save a file.

I’d start with Option 1 first—it’s the simplest, official solution that doesn’t require any extra extensions or npm packages. Happy coding!

内容的提问来源于stack exchange,提问作者raju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:43:36