Protecting secrets, hardening environment variables, and securing your application is a critical part of the development process. Being able to quickly and easily manage your secrets and environment variables is a key part of this process.

sha_env is a simple, yet clever, library to ease the management of .env files in a dart/ flutter project. The goal of the library isn't to provide a state-of-the-art solution to managing secrets, but rather to provide a simple and easy-to-use solution that can be used in a variety of projects.

Features

  • Load environment variables from a .env file using one line of code.
  • A simple API to get and use environment variables in your project.
  • Possibility to create a custom parser to create complex objects from environment variables.

Installation

Add the following to your pubspec.yaml file:

dependencies:
  sha_env: ^3.0.0

or run the following command:

$ dart pub add sha_env

Usage

.env file format

The file format is simple and easy to understand. Each line in the file should be a key-value pair separated by an equal sign =.

KEY=VALUE

Keys will be converted to uppercase and can only contain letters, numbers, and underscores. Values can be any string.

You can also add comments to your .env file by starting a line with a # character.

# This is a comment
KEY=VALUE
KEY_2=VALUE # you can also add comments at the end of a line
#COMMENTED_KEY=VALUE # This line will be ignored

You can also infer values from other environment variables by using the ${} syntax.

# This is a comment
KEY=VALUE
ANOTHER_KEY=${KEY}

An incorrectly formatted line will be ignored.

# This is a comment
KEY=VALUE
ANOTHER_KEY=${KEY}
INVALID LINE #This line will be ignored

Loading environment variables

To load environment variables from a .env file, you can use the load function.


import 'package:sha_env/sha_env.dart';

Future<void> main(final List<String> args) async {

  /// The call to [load] should always be near as possible to the entry point of your application.
  await load();

  /// [env] is a global variable that holds all the environment variables.
  print(env['KEY']); // VALUE

  /// You can also use the [environment] getter to access the environment variables.
  print(environment['ANOTHER_KEY']); // VALUE
}

If your application is using Isolate you should call the load function in the entry point of the isolate. This is required since the child isolate doesn't share the same memory space as the main isolate.

import 'package:sha_env/sha_env.dart';

Future<void> isolateEntryPoint() async {
  await load();

  print(env['KEY']); // VALUE
}

A more complete example can be found in the example directory.

Custom parser

You can create a custom parser to create complex objects from environment variables.


import 'package:sha_env/sha_env.dart';

Future<void> main(final List<String> args) async {

  /// The call to [load] should always be near as possible to the entry point of your application.
  await load();

  /// We transform HOST_URI into an [Uri] by using a lambda.
  Uri hostUri = fromEnvironment<Uri>('HOST_URI', (value) => Uri.parse(value));

  /// Fancy stuff here
  var result = await http.get(hostUri);

  print(result.body);

  exit(0);
}

License and AI Usage

This project has been written without the use of any AI or machine learning models.

Per-project license, it's forbidden to use any AI or machine learning models to generate any part of this project OR to train any AI or machine learning models using any part of this project.

Libraries

sha_env