2

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?

enter image description here

Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25

2 Answers2

3

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:

Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25
3

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

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53