![](/img/trans.png)
[英]Navigator operation requested with a context that does not include a Navigator.(Flutter)
[英]Flutter Navigator operation requested with a context that does not include a Navigator. on splash screen
所以我試圖實現一個閃屏,有兩個原因。
我正在使用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,
);
}
}
這是來自app.dart
的App()
:
class _AppState extends State<App> {
//Contains the simple drawer with switch case for navigation
//Taken directly from the flutter docs
}
基本思想是加載啟動屏幕 4000 毫秒,在此期間應用程序將加載所有必要的數據,然后導航到包含導航路線和所有內容的 App()。 但由於某種原因,我收到Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
我無法確定原因是什么,因為看不到您的整個應用程序結構,但我猜原因是您的應用程序根目錄中沒有MaterialApp()
。
如果這不起作用,請更新您的問題以包含更多有關如何顯示初始屏幕應用程序的代碼。
這是因為您使用了return Timer()所以刪除了return關鍵字,所以嘗試將您的代碼更改為:
Future navigateToHomeScreen() async {
Timer(
const Duration(milliseconds: 4000),
() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (BuildContext context) => App()));
},
);
}
或者,您可以使用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.