env_reader
Leia, encripte ou gere variáveis de ambiente de um arquivo .env em um modelo Dart obfuscado.
Leia, encripte ou gere variáveis de ambiente de um arquivo .env em um modelo Dart obfuscado.
^2.4.2^2.2.0^1.1.0Texto original em inglês. Visite o GitHub para a versão atual.
https://user-images.githubusercontent.com/45191605/274121951-21239cc4-37b3-4dd2-864f-d5f528da4e22.png
https://img.shields.io/pub/v/env_reader.svg?logo=flutter&color=blue&style=flat-square
Read, encrypt, or generate environment variables from .env file into an obfuscated Dart model.
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
Now elevate your development experience with these straightforward steps:
Start by crafting your .env file in the root directory of your project, right alongside your trusty pubspec.yaml.
API_KEY=VYIUJ7tLdJFqrBesnOJEpkbceBB5GNz0t1aYgHxK3BMxbJOc/g==
DEBUG=true
PORT=8080
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
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 🎉
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')),
"MyOptionalSecretKey");
// Or you can load by creating your own `EnvReader` instance.
EnvReader production = EnvReader();
await production.load(
EnvStringLoader(await rootBundle.loadString('assets/env/.env')),
"MyOptionalSecretKey");
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;
Text(
text:
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
Text(
text:
EnvModel.debug ? "🤫 pssst, this is my api key y'all \n\n ${EnvModel.apiKey}" : "Nothing to see here 🤪",
);
Available commands:
| Flag | Description |
|---|---|
| -i, --input (mandatory) | Input path of the .env file |
| -o, --output | Output path for the encrypted .env file |
| -s, --key | Secrey key for encryption & decryption |
| --model | Generate dart model file to your desired file path |
| --null-safety | Make the model null safety |
| --[no-]obfuscate | Obfuscating generated values of model |
| (defaults to on) | |
| --[no-]pubspec | Insert asset path to pubspec.yaml |
| (defaults to on) | |
| --[no-]gitignore | Insert .env input & output file into .gitignore |
| (defaults to on) | |
| -h, --help | Print usage information |
Example usage:
env_reader -i ".env" -o "assets/env/" -s "MyOptionalSecretKey" --model="lib/src/env_model.dart" --null-safety