Reputation: 7110
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?
Upvotes: 8
Views: 6033
Reputation: 12345
Correct version is
{
"version": "0.0.1",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "debug"
},
{
"name": "Flutter (profile)",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "profile"
},
{
"name": "Flutter (release)",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "release"
},
{
"name": "Flutter (dev)",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "debug"
},
{
"name": "Flutter (dev profile)",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "profile"
},
{
"name": "Flutter (dev release)",
"request": "launch",
"type": "dart",
"program": "lib/main.dart",
"args": [
"--flavor",
"dev",
"--dart-define",
"app.flavor=dev",
],
"flutterMode": "release"
},
]
}
Upvotes: 2
Reputation: 42433
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.
Upvotes: 10