搜索插件、代码仓与 APP 分类

插件详情v2.1.0

env_reader

从 .env 文件中读取、加密或生成环境变量,并将其放入混淆的 Dart 模型中。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
82
喜欢数
23
Pub 分数
140

安装插件

flutter pub add env_reader

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

env_reader 是一个 Dart 包,可通过从 .env 文件读取、加密或生成环境变量来实现安全处理。它将这些变量转换为混淆的 Dart 模型,通过防止敏感数据暴露来增强安全性。适用于多个平台,支持 Android、iOS、Web、macOS、Windows 和 Linux。通过简单的 pub add 命令可无缝集成到 Flutter 项目中。非常适合在生产环境和开发环境中重视配置安全性的开发者。

适合场景

  • 安全地管理 API 密钥
  • 保护数据库凭据
  • 在CI/CD流水线中处理应用密钥
  • 启用加密配置文件
  • 安全共享环境设置
  • 自动化环境变量生成

主要特点

  • 安全读取 .env 文件
  • 加密环境变量
  • 生成混淆的 Dart 模型
  • 支持多平台部署
  • 通过 pub 提供轻松集成
  • 提供加密和解密工具

适合对象

  • 专注于应用安全的开发者
  • 使用基于环境的配置
  • 需要密钥管理的项目
  • 包含敏感数据的 Flutter 应用
  • 需要安全环境处理的 CI/CD 工作流
  • 跨多个平台部署的应用程序

使用建议

  • 需要妥善的加密密钥管理
  • 初始设置涉及配置步骤
  • 混淆不能替代完整加密
  • 不适合高度动态的运行时密钥
  • 依赖外部 .env 文件的存在
  • 可能需要额外的构建过程步骤

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。


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.

API_KEY=VYIUJ7tLdJFqrBesnOJEpkbceBB5GNz0t1aYgHxK3BMxbJOc/g==
DEBUG=true
PORT=8080
DATABASE_URL=postgresql://user:password@localhost:5432/mydb

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')),
  "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");

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;

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 🤪",
);

Env Reader Command 🚀

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

Example 🚀

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目

暂无相关数据。