我有一个启动器屏幕,我可以在其中检查我是否有特定数据。根据结果,我显示不同的屏幕
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LauncherScreen()
);
}
}
class LauncherScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("build, build");
Future.delayed(new Duration(milliseconds: 2000), () {
LocalData localData = LocalData();
localData.getCity().then((city) {
if (city != null) {
Const.city = city;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Home()),
);
} else {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SelectCities()),
);
}
});
});
return Container(
width: double.infinity,
height: double.infinity,
color: Color(GoEngColors.violet));
}
}
我LauncherScreen
叫了两次,结果我看到我的Home
或Select city
屏幕出现了两次。
这种行为的原因是什么以及如何解决?谢谢。