I’m trying to use –dart-define but it isn’t working.
I have tried flutter run --dart-define=ENV=Value
And also this vs code launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Dev",
"program": "${workspaceFolder}/lib/main_development.dart",
"request": "launch",
"type": "dart",
"toolArgs": [
"--flavor",
"development",
"--dart-define",
"ENV=development",
"--target",
"lib/main_development.dart",
"-v"
],
},
{
"name": "Qa",
"program": "lib/main_qa.dart",
"request": "launch",
"type": "dart",
"toolArgs": [
"--flavor",
"qa",
"--dart-define",
"ENV=qa",
"--target",
"lib/main_qa.dart",
"-v"
],
}
]
}
Then on the first run:
const env = String.fromEnvironment('ENV');
is always "".
I’m on mac 13.4.1 (c) (22F770820d). I need to do this because iOS seems to run main.dart on the first run, not main_development.dart/main_qa.dart. Waiting 5 seconds does not cause it to work. Hitting refresh causes main_development.dart/main_qa.dart to run, and also const env = String.fromEnvironment('ENV')
to work.
The issue is not about syntax, because I changed my dev launch settings to @Riyan’s answer and kept my qa launch settings the same. And they both work, but only after an app refresh.
I am using both the iOS simulator and physical iOS device. It behaves the same in release mode.
2
Answers
Found the dart guide for environment variables environment-declarations
Code :
CLI:
Android Studio Run/Debug Configuration:
VS Code launch.json:
Additional to @Riyan answer,
One interesting thing I found lately.
adding
const
somehow makes it work.