Flutter 使用不包含导航器的上下文请求的颤振导航器操作。在闪屏上
因此,我试图实现一个启动屏幕,其中有两个原因Flutter 使用不包含导航器的上下文请求的颤振导航器操作。在闪屏上,flutter,dart,Flutter,Dart,因此,我试图实现一个启动屏幕,其中有两个原因 指定给启动屏幕的时间将用于加载所有数据 美化 我正在使用 这是我的代码: class _SplashScreenState extends State<SplashScreen> { void initState() { super.initState(); navigateToHomeScreen(); } Future navigateToHomeScreen() async { return
class _SplashScreenState extends State<SplashScreen> {
void initState() {
super.initState();
navigateToHomeScreen();
}
Future navigateToHomeScreen() async {
return Timer(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App())); ---> doesn't go to new screen
},
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Color(0xff75c760),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildAppName(),
SizedBox(height: 20),
_buildSpinner(),
],
),
),
);
}
Widget _buildAppName() {
return Text(
"MasterChef",
style: GoogleFonts.robotoMono(
color: Colors.black,
fontStyle: FontStyle.normal,
fontSize: 30,
fontWeight: FontWeight.bold,
),
);
}
Widget _buildSpinner() {
return SpinKitDoubleBounce(
color: Colors.white,
);
}
}
其基本思想是加载启动屏幕4000毫秒,在此期间应用程序将加载所有必要的数据,然后导航到包含导航路线和所有内容的app()。但是由于某些原因,我得到了未处理的异常:在不包含导航器的上下文中请求了导航器操作。我无法确定原因是什么,因为我看不到您的整个应用程序结构,但我猜原因是您的应用程序根目录中没有
MaterialApp()
如果这不起作用,请更新您的问题,以包含有关如何显示启动屏幕应用程序的更多代码。这是因为您使用了return Timer(),因此请删除return关键字,然后尝试将代码更改为:
Future navigateToHomeScreen() async {
Timer(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}
或者,您可以使用Future而不是Timer
谢谢,但没用。仍然在没有包含导航器的上下文的情况下请求导航器操作。我相信您已经得到了答案,我不认为您会错过MaterialApp。不用担心。嘿,这确实有效。由于我将我的
runApp(App())
更改为runApp(SplashScreen())
,因此没有MaterialApp()
。谢谢你的快速解决,没问题,随时都可以。
Future navigateToHomeScreen() async {
Timer(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}
Future navigateToHomeScreen() async {
Future.delayed(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}