如何解决例外:错误状态:未来已经完成
在主页的初始状态检查版本不匹配时,我遇到了上述错误, 这是我的代码:
@override
void initState() {
getCategoriesName();
checkInternetConnection();
try {
versionCheck(context);
} catch (e) {
print(e);
}
super.initState();
}
versionCheck(BuildContext context) async {
//Get Current installed version of app
final PackageInfo info = await PackageInfo.fromPlatform();
double currentVersion = double.parse(info.version.trim().replaceAll(".",""));
print("Current Version--------------------");
print(info.version);
//Get Latest version info from firebase config
final RemoteConfig remoteConfig = await RemoteConfig.instance;
try {
// Using default duration to force fetching from Remote Server.
await remoteConfig.fetch(expiration: const Duration(seconds: 0));
await remoteConfig.activateFetched();
remoteConfig.getString('force_update_current_version');
double newVersion = double.parse(remoteConfig
.getString('force_update_current_version')
.trim()
.replaceAll(".",""));
print("New Version-----------------------");
print(remoteConfig.getString('force_update_current_version'));
if (newVersion > currentVersion) {
_showVersionDialog(context);
}
} on FetchThrottledException catch (exception) {
// Fetch throttled.
print(exception);
} catch (exception) {
print('Unable to fetch remote config. Cached or default values will be used');
}
}
_showVersionDialog(context) async {
await showDialog<String>(
context: context,barrierdismissible: false,builder: (BuildContext context) {
String title = "New Update Available";
String message =
"There is a newer version of app available please update it Now.";
String btnLabel = "Update Now";
String btnLabelCancel = "Later";
return new AlertDialog(
title: Text(title),content: Text(message),actions: <Widget>[
FlatButton(
child: Text(btnLabel),onpressed: () => _launchURL(PLAY_STORE_URL),),FlatButton(
child: Text(btnLabelCancel),onpressed: () => Navigator.pop(context),],);
},);
}
每次我运行代码时,它都会打印 3 次我在检查版本不匹配时打印的当前版本和新版本。在搜索了错误的所有可能原因后,我发现 showdialog 是罪魁祸首,我已经尝试了一切可能的方法来修复它,例如将 showdialog 包围在
WidgetsBinding.instance.addPostFrameCallback((_){
});
或
SchedulerBinding.instance.addPostFrameCallback((_) {
});
在上面的代码包围之后,我收到这个错误 Flutter:查找已停用小部件的祖先是不安全的
解决方法
我认为您需要从 try-catch
中删除 initState
块,然后尝试使用
@override
void initState() {
getCategoriesName();
checkInternetConnection();
versionCheck(context); // CHeck here
super.initState();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。