Skip to content

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:

  1. Given a .env file or system environment variables (e.g export VAR=test)
  2. And a dart class like:
    import 'package:envied/envied.dart';
    part 'env.g.dart';
    @envied
    abstract class Env {
    @EnviedField(varName: 'KEY')
    static const key = _Env.key;
    }
  3. build_runner will generate a env.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

Terminal window
# Uses the debug env
dart run build_runner build
# Uses the release env
dart 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

  1. Modify the key in .env and/or .env.debug

  2. Clean and Run the build_runner (why clean?)

    Terminal window
    dart run build_runner clean
    dart run build_runner build --delete-conflicting-outputs
  3. Update the GitHub secret value

  4. Run the app again. The key value should be updated.

Creating a key

Make sure to follow these steps to create a new key.

  1. Create a new key in .env and .env.debug

    KEY_NEW=123
  2. Create a new static variable in lib/env/app_env.dart. For example:

    @EnviedField(varName: 'KEY_NEW')
    @override
    static const String keyNew = _Env.keyNew;
  3. Run the build_runner

    Terminal window
    dart run build_runner build
  4. Add the new key to ALL the GitHub workflows that uses envionment variables

    env:
    ...
    KEY_NEW: ${{ secrets.KEY_NEW }}
  5. Store the key in GitHub secrets

  6. Use the new key (e.gEnv.keyNew) in your code