dotenv
4.2.0, released 3 years ago by practicalflutter.com
Load environment variables at runtime from a .env file.
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.
- cli_config
Alternative approach for loading CLI configuration and settings.
50Compare
Works with
Packages commonly used together with dotenv: ones it builds on, ones built on it, and its own family.
- argsdotenv depends on itLibrary for parsing raw command-line arguments into options and values using GNU and POSIX styles.
- flutter_secure_dotenv_generatorBuilt on dotenvA development dependency to generate secure dotenv file for flutter_secure_dotenv package.
- supadartBuilt on dotenvCLI tool that generates type-safe Dart and Flutter classes from your Supabase database schema.
- firebase_adminBuilt on dotenvPure Dart Firebase admin SDK for authentication and Realtime Database.