如何将远程配置集成到Flutter应用程序中?

4

我需要将 Firebase 远程配置集成到我的 Flutter 应用程序中。尽管在各种网站上进行了搜索,但我并没有找到完整的解决方案。

class RemoteConfigurartion{
  Future<RemoteConfig> setupRemoteConfig() async {
    String value =null;
    final RemoteConfig remoteConfig = await RemoteConfig.instance;

    remoteConfig.setConfigSettings(RemoteConfigSettings(debugMode: false));
    remoteConfig.setDefaults(<String, dynamic>{
      'riddle': "off",
    });
    try {
      // Using default duration to force fetching from remote server.
      await remoteConfig.fetch(expiration: const Duration(seconds: 0));
      await remoteConfig.activateFetched();
    } on FetchThrottledException catch (exception) {
      // Fetch throttled.
      print(exception);
    } catch (exception) {
      print(
          'Unable to fetch remote config. Cached or default values will be '
              'used');
    }
    return remoteConfig;
  }
}

这是我已经发现的,这是我得到的结果:

在通道plugins.flutter.io/firebase_remote_config上找不到RemoteConfig#instance方法的实现

但我已经在pubspec.yaml和android gradle文件夹中添加了所有插件。
有人能帮我找到一个完整的解决方案来集成远程配置到Flutter应用程序吗?

你分享的代码有什么问题? - Frank van Puffelen
在通道plugins.flutter.io/firebase_remote_config上找不到RemoteConfig #instance方法的实现,这是我得到的结果,但我已经在pubspec.yaml和android gradle文件夹中添加了所有插件。@FrankvanPuffelen - Jithin Joy
你是否重新构建了应用程序,但仍然遇到了这个错误?"MethodChannel was not found" 意味着你在运行应用程序时添加了插件到 pubspec.yaml。Flutter 可以热重载 Dart 代码,但不能热重载 Android、iOS 代码。因此,当你添加的插件使用平台代码(例如:方法通道)时,你需要重新构建应用程序。 - Phani Rithvij
你是否已经启用了Firebase远程配置API? https://console.cloud.google.com/apis/api/firebaseremoteconfig.googleapis.com/ - RyosukeOK
1个回答

0
请确保已初始化firebase_core,对于Android而言,google-services.json应该在app文件夹中。除此之外,在您的代码中似乎没有任何问题。您可能在设置过程中错过了一些步骤。
如果您刚刚添加了插件,最好使用重启而不是热重载来运行应用程序,以确保所有最近添加的插件都包含在构建中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接