I'm using the envied_generator
and build_runner
packages. I want to modify the path within @Envied(path: const String.fromEnvironment('environment'))
based on the environment.
I launch my Flutter app through VS Code launch configuration which triggers a prelaunch task (dart run build_runner build --delete-conflicting-outputs
). However, const String.fromEnvironment('environment')
always returns an empty value. I attempted to pass the environment variable using the --define
flag, but it didn’t work.
How can I correctly pass environment variables to String.fromEnvironment()
when running build_runner
?
UPD:
My launch.json
file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Dev-Debug",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": ["--flavor", "dev",
"--dart-define", "environment=dev"],
"preLaunchTask": "build_runner_dev"
}
]
}
My tasks.json
file:
{
"version": "2.0.0",
"tasks": [
{
"label": "build_runner_dev",
"type": "shell",
"command": "dart run build_runner build -- --define=environment=dev",
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
I tried the command option you are showing above:
dart run build_runner build -- --define=environment=dev
and the constant env
is always the empty string:
final env = const String.fromEnvironment('environment'); // Empty String
However, build_runner
has its own --define
option, see How is the configuration for a builder resolved?.
The default options for envied
are listed below:
targets:
$default:
builders:
envied_generator|envied:
options:
path: .env.custom
override: true
To set the option path
from the command line try:
$ dart run build_runner build --define=envied_generator:envied=path="custom_path" --define=envied_generator:envied=override=true
This should overwrite the default build option set in the build.yaml
file.
Updated the command, see Drompai's comment below.