I am trying to run a preLaunchTask to define environment vars. The problem is that the tasks runs in a separate terminal while the flutter launch runs in the Debug Console.
Is there a way to launch the tasks in the debug console?
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "LoadEnvVars",
"command": "pwsh.exe",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/load_env_vars.ps1"
],
},
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "makon_front (pwsh)",
"request": "launch",
"type": "dart",
"args": ["--dart-define",
"FIREBASE_WEB_CONFIG=${env:FIREBASE_CONFIG_WEB}",
"--dart-define",
"USE_FB_EMULATOR=${env:USE_FB_EMULATOR}"],
"preLaunchTask": "LoadEnvVars"
},
{
"name": "makon_front (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "makon_front (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
],
}
I have tested the actual string in dart pad, and it works fine. I have also ran const bool.hasEnvironment
in the Debug Console, it returns a false
. Clearly the env vars are not loading in the debug console before dart launch. How do I do that?
2
Answers
Well my original goal was to load env vars through file. In Flutter 3.7, they have added the
--dart-define-from-file
, which allows you to define your environment in a json file.The Debug Console is not a terminal, so apps run there cannot run in the same terminal as tasks. While it’s possible to have Dart CLI scripts run in the built-in terminal, this isn’t supported for Flutter (because the debugger needs to own the
flutter run
process) – although if it were, I still don’t believe you could have VS Code reuse a Task terminal for a debug session.The best way to do what you want is (as you found) definitely
--dart-define-from-file
which was added in the last version.