philiprehberger_env

Tests pub package Last updated

Dotenv file parser with typed getters and multi-environment support

Requirements

  • Dart >= 3.6

Installation

Add to your pubspec.yaml:

dependencies:
  philiprehberger_env: ^0.1.0

Then run:

dart pub get

Usage

import 'package:philiprehberger_env/philiprehberger_env.dart';

final env = Env.fromString('PORT=8080\nDEBUG=true');
print(env.getInt('PORT'));   // 8080
print(env.getBool('DEBUG')); // true

Parsing .env Files

final content = '''
# Database config
DB_HOST=localhost
DB_PORT=5432
DB_NAME=myapp
DEBUG=true
TAGS=web,api,auth
''';

final env = Env.fromString(content);
print(env.getString('DB_HOST')); // localhost

Typed Getters

final env = Env.fromString('PORT=8080\nDEBUG=true\nTAGS=a,b,c');

env.getString('PORT');                          // "8080"
env.getInt('PORT');                             // 8080
env.getBool('DEBUG');                           // true
env.getList('TAGS');                            // ["a", "b", "c"]
env.getString('MISSING', defaultValue: 'n/a'); // "n/a"
env.has('PORT');                                // true

Variable Expansion

final env = Env.fromString('HOST=localhost\nURL=http://\${HOST}:8080');
print(env.getString('URL')); // http://localhost:8080

Quoted Values

final env = Env.fromString('MSG="hello world"\nPATH=\'/usr/bin\'');
print(env.getString('MSG'));  // hello world
print(env.getString('PATH')); // /usr/bin

API

Method Description
Env(Map<String, String> values) Create an Env from a pre-parsed map
Env.fromString(String content) Parse a .env file content string
getString(String key, {String? defaultValue}) Get a string value
getInt(String key, {int? defaultValue}) Get an integer value
getBool(String key, {bool? defaultValue}) Get a boolean (true/1/yes/on)
getList(String key, {String separator, List<String>? defaultValue}) Get a list by splitting on separator
has(String key) Check if a key exists
toMap() Get all values as a map
DotenvParser.parse(String content) Parse .env content into a Map<String, String>

Development

dart pub get
dart analyze --fatal-infos
dart test

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Libraries

env
philiprehberger_env
Dotenv file parser with typed getters and multi-environment support.