Env Reader


Enhance the rock-solid integrity of your .env configuration by seamlessly encrypting and decrypting data sourced from a dynamic range of originsโ€”be it assets, files, strings, memory, or networksโ€”spโ€ฆ



Env Reader

Read, encrypt, or generate environment variables from .env file into an obfuscated Dart model.

Features ๐Ÿš€

  • Automated Generation: Transform your .env files into dynamic Dart models directly. No need to add annotation. โœจ
  • Seamless Integration: Directly update your pubspec.yaml and .gitignore on command. No need manual labor. ๐Ÿ› ๏ธ
  • Fortified Encryption: Shield your precious .env configurations with an encryption. Say no to prying eyes.๐Ÿ”’
  • Data Diversity Unleashed: Whether they're integers, decimals, booleans, or strings. Automatic interpretation is at your service. ๐ŸŽฎ
  • Versatile Sourcing: Load your .env from various sources: assets, files, memory, network, and strings. The choice is yours. ๐Ÿ”„

Install ๐Ÿš€

Get started with these quick commands:

๐Ÿ”ฅ Add env_reader to your pubspec.yaml with a single line:

dart pub add env_reader

โœจ Unlock the magic by activating the env_reader CLI:

dart pub global activate env_reader

Usage ๐Ÿš€

Now elevate your development experience with these straightforward steps:

1. Set up your configuration

Start by crafting your .env file in the root directory of your project, right alongside your trusty pubspec.yaml.


2. Run the command (Optional)

Now, if you want to generate encrypted env file, run this command in your terminal:

env_reader --input=".env" --output="assets/env/" --key="MyOptionalSecretKey"

[!NOTE] output: .env successfully encrypted into assets/env/.env ๐Ÿš€

also if you want to generate dart model from this env file, use tihs:

env_reader --input-".env" --model="lib/src/env_model.dart" --null-safety

[!NOTE] output: .env successfully generated into lib/src/env_model.dart ๐ŸŽ‰

3. Loading your .env

Load the env_reader instance:

import 'package:env_reader/env_reader.dart';
import 'package:flutter/services.dart';

await Env.load(
  EnvStringLoader(await rootBundle.loadString('assets/env/.env')),

// Or you can load by creating your own `EnvReader` instance.

EnvReader production = EnvReader();
await production.load(
  EnvStringLoader(await rootBundle.loadString('assets/env/.env')),

4. Access your configuration

To get and read the value of your env:

import 'package:env_reader/env_reader.dart';
import 'package:my_package/src/env_model.dart';

String api = Env.read("API_KEY") ?? "Got'cha ๐Ÿ˜Ž";
bool debug = Env.read<bool>("DEBUG") ?? false;

// If you make your own instance, call it like this

String api = production.read("API_KEY") ?? "Got'cha ๐Ÿ˜Ž";
bool debug = production.read<bool>("DEBUG") ?? false;

    debug ? "๐Ÿคซ pssst, this is my api key y'all \n\n $api" : "Nothing to see here ๐Ÿคช",

// Or you can access the value directly from env generated model earlier

    EnvModel.debug ? "๐Ÿคซ pssst, this is my api key y'all \n\n ${EnvModel.apiKey}" : "Nothing to see here ๐Ÿคช",

Env Reader Command ๐Ÿš€

Available commands:

-i, --input (mandatory)Input path of the .env file
-o, --outputOutput path for the encrypted .env file
-s, --keySecrey key for encryption & decryption
--modelGenerate dart model file to your desired file path
--null-safetyMake the model null safety
--[no-]obfuscateObfuscating generated values of model
(defaults to on)
--[no-]pubspecInsert asset path to pubspec.yaml
(defaults to on)
--[no-]gitignoreInsert .env input & output file into .gitignore
(defaults to on)
-h, --helpPrint usage information

Example usage:

env_reader -i ".env" -o "assets/env/" -s "MyOptionalSecretKey" --model="lib/src/env_model.dart" --null-safety

Example ๐Ÿš€