Skip to content
pubscout

dotenv

4.2.0, released 3 years ago by practicalflutter.com

Load environment variables at runtime from a .env file.

$ flutter pub add dotenv
Source repository

What it does

Parses a .env file to load configuration values into your application at runtime. It can optionally merge these values with the built-in Platform.environment map and supports variable substitution.

AI-written summary of version 4.2.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to store configuration in environment variables per the twelve-factor app methodology.
  • You want to mimic a production environment during local development using a .env file.

Skip it when

  • You require compile-time environment configuration injected via dart --define.
  • You need secure persistent key-value device storage for user data.

Strengths

  • Easy to configure with support for multiple file paths and variable substitution.
  • Includes a command-line interface helper to initialize and test .env files.

Weaknesses

  • Does not expose the underlying map directly since version 4.0.0, requiring forwarding methods.
  • Does not inject variables at compile time.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2

Create a .env file in your project and optionally add it to your .gitignore.

import 'package:dotenv/dotenv.dart';

void main() {
  var dotEnv = DotEnv(includePlatformEnvironment: true)..load();
  var apiKey = dotEnv['API_KEY'];
}

Alternatives

Packages that do the same job and could replace dotenv.

Works with

Packages commonly used together with dotenv: ones it builds on, ones built on it, and its own family.