[英]Flutter Navigator operation requested with a context that does not include a Navigator. on splash screen
so I'm trying to implement a splash screen where for two reasons.所以我试图实现一个闪屏,有两个原因。
I'm using flutter_spinkit So here's my code:我正在使用flutter_spinkit所以这是我的代码:
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,
);
}
}
and here's the App()
from app.dart
:这是来自
app.dart
的App()
:
class _AppState extends State<App> {
//Contains the simple drawer with switch case for navigation
//Taken directly from the flutter docs
}
The basic idea is to load the splash screen for 4000 milliseconds in which the app will load all the necessary data and then navigate to App() which contains the navigation routes and all.基本思想是加载启动屏幕 4000 毫秒,在此期间应用程序将加载所有必要的数据,然后导航到包含导航路线和所有内容的 App()。 But for some reason I'm getting
Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
但由于某种原因,我收到
Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
I cannot be certain to what the cause is as cannot see your entire app structure but I am guessing the cause is that you do not have a MaterialApp()
at the root of your app.我无法确定原因是什么,因为看不到您的整个应用程序结构,但我猜原因是您的应用程序根目录中没有
MaterialApp()
。
If this doesn't work please update your question to contain more code of how you get the splash screen app to be displayed.如果这不起作用,请更新您的问题以包含更多有关如何显示初始屏幕应用程序的代码。
This is because you used return Timer() so remove the return keyword so, try changing your code to:这是因为您使用了return Timer()所以删除了return关键字,所以尝试将您的代码更改为:
Future navigateToHomeScreen() async {
Timer(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}
Alternatively, you can use Future instead of Timer或者,您可以使用Future而不是Timer
Future navigateToHomeScreen() async {
Future.delayed(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.