Flutter 的远程配置功能管理器

[License: MIT]

功能管理器允许您向用户隐藏一些未完成/秘密功能或实验,这些功能可以从远程数据源或本地设置进行管理。

如果您只需要本地功能开关或偏好设置,请使用 Feature Manager

Example 01 Example 02

入门

安装

添加

remote_config_feature_manager : ^lastest_version

到您的 pubspec.yaml,然后运行

flutter packages get

在您的项目根目录中。

在您的项目中设置 Firebase 远程配置。

要开始使用 Firebase 远程配置 for Flutter,请参考 文档,可在 https://firebase.flutter.dev 找到

基本用法

创建功能列表

创建您将存储功能列表的文件,并在其中创建功能。不要忘记指定来自远程配置的 **remoteSourceKey**。

import 'package:remote_config_feature_manager/remote_config_feature_manager.dart';

class Features {
  static const Feature textFeature = Feature(
    key: 'dev-prefs-text-pref',
    remoteSourceKey: 'remote_prefs_text_pref',
    title: 'Text pref',
    description: 'This is text preference',
    defaultValue: '',
    valueType: FeatureValueType.text,
  );

  static const Feature booleanFeature = Feature(
    key: 'dev-prefs-bool-pref',
    remoteSourceKey: 'remote_prefs_bool_pref',
    title: 'Toggle pref',
    description: 'This is toggle preference',
    defaultValue: false,
    valueType: FeatureValueType.toggle,
  );

  static const Feature doubleFeature = Feature(
    key: 'dev-prefs-double-pref',
    remoteSourceKey: 'remote_prefs_double_pref',
    title: 'Number double pref',
    description: 'This is number double preference',
    defaultValue: 0.0,
    valueType: FeatureValueType.doubleNumber,
  );

  static const Feature integerFeature = Feature(
    key: 'dev-prefs-integer-pref',
    remoteSourceKey: 'remote_prefs_integer_pref',
    title: 'Number integer pref',
    description: 'This is number integer preference',
    defaultValue: 0,
    valueType: FeatureValueType.integerNumber,
  );

  static const Feature jsonFeature = Feature(
    key: 'dev-prefs-json-pref',
    remoteSourceKey: 'remote_prefs_json_pref',
    title: 'Json pref',
    description: 'This is json preference',
    defaultValue: """{"value": "Json default value"}""",
    valueType: FeatureValueType.json,
  );

  static const List<Feature> values = <Feature>[
    Features.textFeature,
    Features.booleanFeature,
    Features.doubleFeature,
    Features.integerFeature,
    Features.jsonFeature,
  ];
}

激活功能管理器

要创建 RemoteConfigFeatureManger,请提供 SharedPreferences 和 FirebaseRemoteConfig 实例。

...
final RemoteConfigFeatureManager featureManager = RemoteConfigFeatureManager(
          sharedPreferences: sharedPreferences,
          firebaseRemoteConfig: remoteConfig,
      );
...

要获取并激活功能值,请使用 **activate** 函数。

...
await featureManager.activate(
            Features.values,
            minimumFetchInterval: const Duration(
                  minutes: 5,
            ),
      );
...

如果您想在其他地方初始化 FirebaseRemoteConfig,只需使用 **refresh** 函数将远程值映射到您的功能即可。

...
featureManager.refresh(Features.values);
...

使用 **RemoteConfigFeatureManager** 检查功能是否已启用。创建 RemoteConfigFeatureManager 实例的首选方法是使用 DI(Provider、GetIt 等)。

...
final bool isEnabled =
    context.read<RemoteConfigFeatureManager>().isEnabled(Features.booleanFeature);
...

在 Firebase 控制台中修改远程功能值

Example 01

在 DEBUG(开发)模式下修改远程功能值

要执行此操作,您只需在应用中的任何位置打开 DeveloperPreferences 屏幕。您应该将功能列表作为参数传递给此屏幕。

附注:您应该为生产版本隐藏此按钮。

Navigator.of(context).push(
  MaterialPageRoute(
    builder: (BuildContext context) =>
      DeveloperPreferencesScreen(Features.values),
    ),
);

功能参数

参数 默认值 描述
key String 必需 此键将用于在本地存储中存储值。
type FeatureType FeatureType.feature 可用于区分本地功能和由某些远程提供程序驱动的实验。
valueType FeatureValueType 必需 功能的价值类型。如果您需要切换,请使用 FeatureValueType.toggle
title String 必需 将在开发者偏好设置屏幕中使用的标题。
remoteSourceKey String 来自远程源的键。
description String 将在开发者偏好设置屏幕中使用的描述。
value Object? Null 功能的存储值。将从本地存储中获取。
defaultValue Object? Null 功能的默认值。如果存储值为 Null,则 FeatureManager 将返回此值。
enum FeatureType { feature, experiment }
enum FeatureValueType { text, toggle, doubleNumber, integerNumber, json }

贡献

欢迎随时与我联系([email protected])或为该存储库创建 Pull Requests/Issues?

GitHub

查看 Github