如何解决Flutter:从控制台更改配置后,立即从远程接收到客户端的新配置
我创建了一个服务来从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控制台更改配置时,我必须停止/启动应用程序以更新我的配置。当我从控制台更改配置时,是否有可能立即从远程接收客户端中的新配置?
解决方法
Firebase远程配置不能像这样使用。它不是实时的东西。但是,如果您要在从Firebase控制台更新配置后立即进行更新,则可以通过提供expiration参数0秒的持续时间来做到这一点。
await remoteConfig.fetch(expiration: const Duration(seconds: 0));
每次都会获取最新值,但不建议这样做,因为您还可以获取FetchThrottledException。
,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();
非常重要。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。