搜尋套件、儲存庫與 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')
    );
  });
}

所屬分類

原始碼儲存庫

使用此套件的專案