Reputation: 1401
In my flutter app, I have two screens namely, AppUpdate and StoreTiming. The AppUpdate screen is shown if the current build is not equal to enforced build (See code below). If current build is equal to enforced build, then the StoreTiming screen is shown. In the case of current build = enforced build, when I start the app, I see the AppUpdate screen appear for about a second and then it is replaced by StoreTming screen. Why does this happen?
void initState(){
super.initState();
_initPackageInfo();
_enforcedVersion();
}
Future<void> _initPackageInfo() async {
final info = await PackageInfo.fromPlatform();
setState(() {
_packageInfo = info;
});
}
Future<void> _enforcedVersion() async {
final RemoteConfig remoteConfig = RemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),
minimumFetchInterval: Duration.zero,
));
await remoteConfig.fetchAndActivate();
setState(() {
enforcedBuildNumber = remoteConfig.getString('enforced_build_number');
});
}
Widget build(BuildContext context) {
return !(_isLoading)?MultiProvider(
providers: [],
child: Consumer<Auth>(builder: (ctx, auth, _) =>
MaterialApp(
navigatorKey: navigatorKey,
title: 'MyAppName',
theme: ThemeData(
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.black,
displayColor: Colors.black,
),
primaryColor: Colors.white,
accentColor: Color(0xFFF2AD18),
fontFamily: 'Muli',
),
home:
_packageInfo.buildNumber!=enforcedBuildNumber?AppUpdateScreen():StoreTimings(),
routes:{},
),)):Center(child:Loading());}}
Upvotes: 0
Views: 119
Reputation: 573
Because you make _initPackageInfo();
and _enforcedVersion();
async and async functions need time to complete their works! So before set a correct value on fields in this function build
method called and show a page. you can await this function before load completely and set correct values then show a page. but while a async functions is doing their own job you must show some thing to user! You can show loading page if you want and if values are ready show a page...
I recommend you to use a state management library like flutter_bloc to have a clean code and better functionallity.
Upvotes: 1