Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to web debug session in VS Code

I need to pass parameter to debug session in VS Code while programming in flutter/dart. I added the following data into launch.json as described in documentation.

{
  "configurations": {
    ..
    // Any custom environment variables to set when running the app with this
    // launch config.
    "env": {
      "DEBUG_MODE": true
    }

    // Arguments to be passed to the Dart or Flutter app.
    "args": [
        "--dart-define", 
        "DEBUG_VALUE=true",
    ],  
}

and tried to read the value so:

void main(List<String> args) {
  final debugMode = String.fromEnvironment('DEBUG_MODE');
  final debugValue = String.fromEnvironment('DEBUG_VALUE');
  ...
}

but variables are empty, and args list is also empty. So please give me advise what I did wrong?

like image 754
BambinoUA Avatar asked Jun 14 '26 12:06

BambinoUA


1 Answers

Are you definitely using const when reading the value? For reasons I don't entirely understand, this seems to only work if the value is const:

With this in my launch.json:

"toolArgs": [
    "--dart-define",
    "FOO=bar",
]

And code like:

final foo1 = String.fromEnvironment('FOO');
final foo2 = const String.fromEnvironment('FOO');
print('FOO: "$foo1" "$foo2"');

The output is

flutter: FOO: "" "bar"

So if String.fromEnvironment is called in a non-const context, it doesn't seem to return the value.

Edit: Actually, in web, not using const results in an exception. But with const, it still works as expected.

like image 134
Danny Tuppeny Avatar answered Jun 17 '26 01:06

Danny Tuppeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!