【问题标题】:Flutter:Immediately received new config in client from remote when changed config from consoleFlutter:从控制台更改配置时,立即从远程客户端收到新配置
【发布时间】:2020-09-22 16:54:55
【问题描述】:

我创建了一个服务来从 Firebase 远程配置中获取配置:

const String _ShowDataBanner = "show_data_banner";
const String _ShowMainBanner = "show_main_banner";
const String _ShowMainColorBanner = "show_main_color_banner";

class RemoteConfigService {
  final RemoteConfig _remoteConfig;
  final defaults = <String, dynamic>{
    _ShowMainBanner: false,
    _ShowMainColorBanner: "0xffcccccc"
  };

  RemoteConfigService({RemoteConfig remoteConfig})
      : _remoteConfig = remoteConfig;

  static RemoteConfigService _instance;
  static Future<RemoteConfigService> getInstance() async {
    if (_instance == null) {
      _instance = RemoteConfigService(
        remoteConfig: await RemoteConfig.instance,
      );
    }
    return _instance;
  }

  String get showMainBanner => _remoteConfig.getString(_ShowDataBanner);

  Future initialise() async {
    try {
      await _remoteConfig.setDefaults(defaults);
      await _fetchAndActivate();
    } on FetchThrottledException catch (e) {
      print("Remote config fetch throttled: $e");
    } catch (e) {
      print(
          "unable to fetch remote config. Catched or default values will be used");
    }
  }

  Future _fetchAndActivate() async {
    // await _remoteConfig.fetch();
    await _remoteConfig.fetch(expiration: Duration(seconds: 0));
    _remoteConfig
        .activateFetched()
        .then((value) => print("---------> ${value.toString()}"));
  }
}

当我从 Firebase 控制台更改配置时,我必须停止/启动应用程序以更新我的配置。当我从控制台更改配置时,是否可以立即从远程客户端接收新配置?

【问题讨论】:

标签: flutter firebase-remote-config


【解决方案1】:

Firebase 远程配置不适合这样使用。它不是实时的。但是,如果您想在从 firebase 控制台更新配置后立即更新,则可以通过提供 0 秒持续时间的过期参数来实现。

await remoteConfig.fetch(expiration: const Duration(seconds: 0));

它每次都会获取最新的值,但不建议这样做,因为你也可以得到 FetchThrottledException。

【讨论】:

    【解决方案2】:
    Future<RemoteConfig> setupRemoteConfig() async {
      final RemoteConfig remoteConfig = RemoteConfig.instance;
      await remoteConfig.ensureInitialized();
      await remoteConfig.fetchAndActivate();
      var hello = remoteConfig.getString("hello");
      print("hello: |${hello}|");
    
      return remoteConfig;
    }
    

    await remoteConfig.fetchAndActivate(); 很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多