flutter_dotenv 3.1.0
flutter_dotenv: ^3.1.0 copied to clipboard

Flutter Android iOS Linux macOS web Windows

Easily configure any flutter application with global variables using a `.env` file.

flutter_dotenv #

Pub Version

Load configuration at runtime from a .env file which can be used throughout the application.

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code... they are a language- and OS-agnostic standard.

About #

This library is a fork of mockturtl/dotenv dart library, initially with slight changes to make it work with flutter.

An environment is the set of variables known to a process (say, PATH, PORT, ...). It is desirable to mimic the production environment during development (testing, staging, ...) by reading these values from a file.

This library parses that file and merges its values with the built-in Platform.environment map.

Usage #

Create a .env file in the root of your project with the example content:

FOO=foo
BAR=bar
FOOBAR=$FOO$BAR
ESCAPED_DOLLAR_SIGN='$1000'

Add the .env file to your assets bundle in pubspec.yaml

  assets:
    - .env

Optionally add the .env file as an entry in your .gitignore if it isn't already

.env*

Load the .env file in main.dart

import 'package:flutter_dotenv/flutter_dotenv.dart' as DotEnv;

Future main() async {
  // NOTE: The filename will default to .env and doesn't need to be defined in this case
  await DotEnv.load(fileName: ".env");
  //...runapp
}

You can then access variables from .env throughout the application

import 'package:flutter_dotenv/flutter_dotenv.dart';

env['VAR_NAME'];

Optionally you could map env after load to a config model to access a config with types.

Advanced usage #

Referencing #

You can reference variables defined above other within .env:

  FOO=foo
  BAR=bar
  FOOBAR=$FOO$BAR

You can escape referencing by wrapping the value in single quotes:

ESCAPED_DOLLAR_SIGN='$1000'

Merging #

You can merge a map into the environment on load:

  await DotEnv.load(mergeWith: { "FOO": "foo", "BAR": "bar"});

You can also reference these merged variables within .env:

  FOOBAR=$FOO$BAR

Usage with Platform Environment #

The Platform.environment map can be merged into the env:

  // For example using Platform.environment that contains a CLIENT_ID entry
  await DotEnv.load(mergeWith: Platform.environment);
  print(env["CLIENT_ID"]);

Like other merged entries described above, .env entries can reference these merged Platform.Environment entries if required:

  CLIENT_URL=https://$CLIENT_ID.dev.domain.com

Discussion #

Use the issue tracker for bug reports and feature requests.

Pull requests are welcome.

Prior art #

license: MIT #

141
likes
110
pub points
97%
popularity

Easily configure any flutter application with global variables using a `.env` file.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

jamesbcollins92@gmail.com

License

MIT (LICENSE)

Dependencies

flutter, meta

More

Packages that depend on flutter_dotenv