I am creating some tests in Flutter, but I need to set an environment variable before running the tests. Is there a way to set an environment variable when running the app from these "Run | Debug" buttons?
Using the --dart-define flag worked for me:
"configurations": [
{
"name": "Launch App",
"request": "launch",
"type": "dart",
"args": [
"--dart-define", "GOOGLE_OAUTH_CLIENT_ID=xxxxx",
"--dart-define", "GOOGLE_OAUTH_CLIENT_SECRET=yyyyy"
],
}
]
And then use it in the code as such:
String.fromEnvironment('GOOGLE_OAUTH_CLIENT_ID');
Note you may need to explicitly specify a const variable or assignment
With the new update to the Dart SDK it is possible: https://dartcode.org/releases/v3-11/
[...] For example, to add CodeLens for a launch config that sets a RELEASE_MODE=true
environment variable to tests in test/integration_tests
:
{
"name": "Current File (release mode)",
"type": "dart",
"request": "launch",
"codeLens": {
// Types of CodeLens to inject
"for": [ "run-test", "run-test-file", "debug-test", "debug-test-file" ],
// Restrict to certain folders
"path": "test/integration_tests",
// Text for CodeLens link (${debugType} will be replaced with "run" or "debug")
"title": "${debugType} (release)"
},
"env": { "RELEASE_MODE": true }
}
This will insert additional CodeLens links into tests, groups and main functions:
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