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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With