mirror of
https://github.com/hanskokx/application_architecture_using_arcane.git
synced 2026-05-14 02:19:06 +02:00
Updated environment variable handling notes
This commit is contained in:
@@ -501,6 +501,40 @@ env=prod make change-env
|
|||||||
|
|
||||||
This would copy `.env.prod` to `.env`. By default, running the command without specifying an environment will use the `.env.default` file. In this way, an infinite number of environments could be created and easily switched between.
|
This would copy `.env.prod` to `.env`. By default, running the command without specifying an environment will use the `.env.default` file. In this way, an infinite number of environments could be created and easily switched between.
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> This method of managing environment variables has known security issues. In later projects, this method has been retired in favor of passing in the environment variables directly to Flutter.
|
||||||
|
> It is recommended to migrate any existing `dot_env` usage to this newer system.
|
||||||
|
|
||||||
|
Nowadays, environment variables are managed by passing the `.env` file into the Flutter/Dart command directly using `--dart-define-from-file=.env`. This requires a couple of changes, both in how the application is launched and how the environment variables are configured in the code.
|
||||||
|
|
||||||
|
If you're using Visual Studio Code, you can simply edit your `launch.json` and include these lines:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"toolArgs": [
|
||||||
|
"--dart-define-from-file=.env"
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
Migrating the code allows you to _remove_ code, which is always nice. The `AppEnv` class can be removed, as it is no longer necessary. Now, your `EnvVar` enum can point directly to the environment varibles:
|
||||||
|
|
||||||
|
```dart
|
||||||
|
enum EnvVar {
|
||||||
|
/// The environment to use for the API calls. Returns either [sandbox] or [prod].
|
||||||
|
///
|
||||||
|
/// Example `.env` configuration:
|
||||||
|
/// ```
|
||||||
|
/// API_ENVIRONMENT="sandbox"
|
||||||
|
/// ```
|
||||||
|
apiEnvironment(String.fromEnvironment("API_ENVIRONMENT")), // Note the direct reference to the environment variable
|
||||||
|
;
|
||||||
|
|
||||||
|
/// The environment variable to use when retrieving the value of this [EnvVar].
|
||||||
|
final String value;
|
||||||
|
|
||||||
|
const EnvVar(this.value);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
#### Accessing Secrets
|
#### Accessing Secrets
|
||||||
|
|
||||||
Accessing the secrets defined in the environment variables can be done via the `AppEnv`, provided they have been mapped to a corresponding `EnvVar` enum item:
|
Accessing the secrets defined in the environment variables can be done via the `AppEnv`, provided they have been mapped to a corresponding `EnvVar` enum item:
|
||||||
|
|||||||
Reference in New Issue
Block a user