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

插件详情v2.0.2

flutter_config

一个插件,可将环境变量暴露给 Flutter 中的 Dart 代码,以及 iOS 和 Android 中的原生代码。为您的 Flutter 应用带来一些 12 因素理念

AndroidiOS
近 30 天下载
2690
喜欢数
211
Pub 分数
140

安装插件

flutter pub add flutter_config

插件介绍

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

flutter_config 是一个 Flutter 插件,可在 Android 和 iOS 的 Dart 和原生代码中无缝访问环境变量。它通过允许开发者将配置外部化,将 12 因子应用原则引入 Flutter,从而在开发、预发布和生产等不同环境中提高安全性和灵活性。该包从平台特定的文件(例如 iOS 上的 info.plist,Android 上的 build.gradle)读取配置,并将其作为常量暴露给 Dart。这有助于避免硬编码敏感值,并简化部署流程。

适合场景

  • 按环境管理 API 端点
  • 安全存储API密钥
  • 在开发/预发布/生产配置之间切换
  • 根据环境自定义应用程序行为
  • 通过配置启用功能标志
  • 与原生代码共享配置

主要特点

  • 支持 Android 和 iOS 平台
  • 从原生项目文件中读取配置
  • 向 Dart 和原生代码暴露变量
  • 遵循 12 因素应用方法论
  • 通过 pub add 轻松集成
  • 无运行时开销

适合对象

  • 使用多个环境的开发人员
  • 需要安全配置管理的团队
  • 需要原生代码访问配置的App
  • 遵循 12 因子原则的项目
  • 具有环境特定设置的 CI/CD 流水线
  • 具有动态功能开关的应用程序

使用建议

  • 配置必须在应用启动前设置
  • 敏感数据不应提交到版本控制
  • 需要在原生项目中手动设置
  • 不适用于运行时配置更改
  • 仅限构建时的静态值
  • 需要为团队入职提供适当的文档

原始 README

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

Config Variables for your Flutter apps

Plugin that exposes environment variables to your Dart code in Flutter as well as to your native code in iOS and Android.

Inspired by react-native-config

Basic Usage

Create a new file .env in the root of your Flutter app:

API_URL=https://myapi.com
FABRIC_ID=abcdefgh

load all environment varibles in main.dart

import 'package:flutter_config/flutter_config.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // Required by FlutterConfig
  await FlutterConfig.loadEnvVariables();

  runApp(MyApp());
}

Now you can access your environment varibles anywhere in your app.

import 'package:flutter_config/flutter_config.dart';

FlutterConfig.get('FABRIC_ID') // returns 'abcdefgh'

Keep in mind this module doesn't obfuscate or encrypt secrets for packaging, so do not store sensitive keys in .env. It's basically impossible to prevent users from reverse engineering mobile app secrets, so design your app (and APIs) with that in mind.


Load Environment Varibles in Swift

First import the plugin

import flutter_config

Then you can use the .env Variable, replace ENV_API_KEY with yours.

flutter_config.FlutterConfigPlugin.env(for: "ENV_API_KEY")

Getting Started

Install the latest version of the plugin

Refer to Android Setup Guide for initial setup and advanced options

No additional setup is required for iOS, however, for advanced usage refer to the iOS Setup Guide

Testing

Whenever you need to use FlutterConfig in your tests, simply use the method loadValueForTesting

import 'package:flutter_config/flutter_config.dart';

void main() {
  FlutterConfig.loadValueForTesting({'BASE_URL': 'https://www.mockurl.com'});
  
  test('mock http client test', () {
    final client = HttpClient(
      baseUrl: FlutterConfig.get('BASE_URL')
    );
  });
}

所属分类

源码仓库

使用此插件的项目