Setup the environment
ShipFlutter uses the dotenv format and strategy for managing environment variables with the help of the envied package.
The concept is simple:
- Given a
.env
file or system environment variables (e.gexport VAR=test
) - And a dart class like:
import 'package:envied/envied.dart';part 'env.g.dart';@enviedabstract class Env {@EnviedField(varName: 'KEY')static const key = _Env.key;}
build_runner
will generate aenv.g.dart
mapping the keys to dart code.
You can then just call Env.key
in your code to get the value.
Setup
The generated project comes with .env.debug
and .env
files with empty values to
allow debug and release envionment. Those files are included in the .gitignore
file
ensuring keys are never included in source control.
To choose between debug or release env use the --release
flag when running the build_runner
# Uses the debug envdart run build_runner build# Uses the release envdart run build_runner build --release
In addition, the provided GitHub workflows uses the GitHub secrets to include the keys in the build when building the project using GitHub actions.
Updating a key
-
Modify the key in
.env
and/or.env.debug
-
Clean and Run the
build_runner
(why clean?)Terminal window dart run build_runner cleandart run build_runner build --delete-conflicting-outputs -
Update the GitHub secret value
-
Run the app again. The key value should be updated.
Creating a key
Make sure to follow these steps to create a new key.
-
Create a new key in
.env
and.env.debug
KEY_NEW=123 -
Create a new static variable in
lib/env/app_env.dart
. For example:@EnviedField(varName: 'KEY_NEW')@overridestatic const String keyNew = _Env.keyNew; -
Run the
build_runner
Terminal window dart run build_runner build -
Add the new key to ALL the GitHub workflows that uses envionment variables
env:...KEY_NEW: ${{ secrets.KEY_NEW }} -
Store the key in GitHub secrets
-
Use the new key (e.g
Env.keyNew
) in your code