【发布时间】:2021-02-26 16:55:08
【问题描述】:
我正在编写代码来检查用户是否安装了我的应用程序的正确版本,否则将他重定向到 Playstore 以更新应用程序。
我正在使用firebase_remote_config 来存储一个变量,以记录我希望用户使用的最低版本。
Package_info 获取有关用户应用的信息。
url_launcher 从应用重定向到 Playstore
我的问题是我检查版本的方法是异步的,它需要在进入应用程序的第一个屏幕之前向用户显示对话框。
我在initState 中执行它。但是 build 方法在我的异步函数结束之前构建了第一个屏幕,并且我的 ShowDialog 没有呈现。
如何在第一次构建之前首先显示我的异步函数的结果?
这是我经过一些更新后的代码,但在导航到另一个屏幕之前没有显示对话框
class Splashscreen extends StatefulWidget {
@override
_SplashscreenState createState() => _SplashscreenState();
}
class _SplashscreenState extends State<Splashscreen> {
@override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
versionCheck(context);
});
}
Future versionCheck(context) async {
//Get Current installed version of app
final PackageInfo info = await PackageInfo.fromPlatform();
double currentVersion =
double.parse(info.version.trim().replaceAll(".", ""));
//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: 10));
await remoteConfig.activateFetched();
remoteConfig.getString('force_update_current_version');
double nVersion = double.parse(remoteConfig
.getString('force_update_current_version')
.trim()
.replaceAll(".", ""));
if(nVersion > currentVersion){
showDialog(
context: context,
builder: (_) => new AlertDialog(
title: new Text("You are not up to date"),
content: new Text("To use the application, you must update it"),
actions: <Widget>[
FlatButton(
child: Text('Go To Store'),
onPressed: () {
_launchURL(PLAY_STORE_URL);
},
)
],
)
);
}
} on FetchThrottledException catch (exception) {
print(exception);
} catch (exception) {
print(
'Unable to fetch remote config. Cached or default values will be used');
}
}
_launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
@override
Widget build(BuildContext context) {
return ScopedModelDescendant<UserModel>(builder: (context, child, model) {
return new SplashScreen(
seconds: 4,
navigateAfterSeconds:
model.isSignedIn ? HomePage() : SuggestLoginPage(),
image: new Image.asset('assets/images/logo.png', fit: BoxFit.cover,),
backgroundColor: Color(0xff131921),
styleTextUnderTheLoader: new TextStyle( ),
photoSize: 100.0,
loaderColor: Colors.white
);
});
}
}
【问题讨论】:
标签: flutter dart asynchronous build