简体   繁体   English

Flutter 使用不包括导航器的上下文请求导航器操作。 在启动画面上

[英]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.所以我试图实现一个闪屏,有两个原因。

  1. the time given to the splash screen will be used to load all the data给予初始屏幕的时间将用于加载所有数据
  2. For beautification为了美化

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.dartApp()

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.

相关问题 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter) - Navigator operation requested with a context that does not include a Navigator.(Flutter) 在从内存中恢复应用程序时显示密码屏幕 [使用不包含导航器的上下文请求的导航器操作。] - show pincode screen on app resume from memory [Navigator operation requested with a context that does not include a Navigator.] 如何修复使用不包含导航器的上下文请求的导航器操作。 ' 在 flutter - How to fix 'Navigator operation requested with a context that does not include a Navigator. ' in flutter 实施导航器时发生异常。 使用不包含导航器的上下文请求的导航器操作 - Exception while implementing navigator. Navigator operation requested with a context that does not include a Navigator 使用不包含 Navigator 的上下文请求的 Navigator 操作。 (生成器 function 不会显示 MainPage()) - Navigator operation requested with a context that does not include a Navigator. (Builder function wont display MainPage()) Flutter:使用不包含导航器的上下文请求的导航器操作 - Flutter : Navigator operation requested with a context that does not include a Navigator 使用不包含 Navigator (Flutter) 的上下文请求的 Navigator 操作 - 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 - Flutter 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM