【问题标题】:Navigator operation requested with a context that does not include a Navigator even though second route has context使用不包含导航器的上下文请求导航器操作,即使第二条路线具有上下文
【发布时间】:2021-09-18 12:59:49
【问题描述】:
 //main.dart file
 import 'package:flutter/material.dart';
 import 'package:messenger/widgets/splash_screen.dart';

 void main() {
  runApp(MyApp());
   }

 class MyApp extends StatelessWidget {


 @override
 Widget build(BuildContext context) {
return MaterialApp(
  
  title: 'Flutter Demo',
  debugShowCheckedModeBanner: false,
  theme: ThemeData(
    scaffoldBackgroundColor: Colors.white,
    appBarTheme: AppBarTheme(
        brightness: Brightness.light,
        color: Colors.white,
        elevation: 0.0,
        iconTheme: IconThemeData(color: Colors.black),
        actionsIconTheme: IconThemeData(color: Colors.black)),
    fontFamily: "Ubuntu",
    primarySwatch: Colors.blue,
    primaryColor: Color(0xff0084FF),
  ),
  
  home: Column(
  
    children: <Widget>[
      SizedBox(height: 30.0,),
      ElevatedButton(
            onPressed: () => navigation(context),
             child: Text("to splash screen"))
      ],
     ),
   );
  }

 navigation(BuildContext context) {
   Navigator.of(context).push(MaterialPageRoute(builder: (context)=>SplashScreen()));
 }
  }


   //SplashScreen.dart file


   import 'package:flutter/material.dart';
  import 'package:flutter/widgets.dart';
  import 'package:messenger/widgets/login_button.dart';

   class SplashScreen extends StatefulWidget {
   @override
  _SplashScreenState createState() => _SplashScreenState();
 }

 class _SplashScreenState extends State<SplashScreen> {
  @override
   Widget build(BuildContext context) {
   double imageSize = MediaQuery.of(context).size.width / 3;
    return Scaffold(

      body: Center(
  
       child: Image.asset(
      "images/logo.png",
      width: imageSize,
      height: imageSize,
      fit: BoxFit.cover,
    ),
  ),
  bottomNavigationBar: Padding(
    padding: EdgeInsets.symmetric(
      horizontal: 16.0,
      vertical: 16.0,
    ),
    child: Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        LoginButton(text: "Continue with Facebook",onPressed: (){

        },
   colour: Color(0xff3b5998),
    leading: Image.asset("images/facebook-logo.png",
   ),
  ),
        
      ],
    ),
  ),
);
 }
  }

使用不包含导航器的上下文请求导航器操作。 也许我没有很好地理解上下文的概念,这就是为什么我得到
错误。为什么会发生这个错误? SplashScreen 的第二个小部件有 语境。但我仍然收到错误消息。我的代码有什么问题吗 错过了吗?

【问题讨论】:

    标签: flutter navigation widget navigator


    【解决方案1】:

    兄弟,在导航器中调用 Navigator.push 不是好方法,请尝试调用特定功能,或者您可以在 ink well 中调用它,所以请删除 **Navigator(BuildContext context)**在那之后它会为你工作。

    【讨论】:

      【解决方案2】:

      **这里是可以解决你的导航问题的代码**

      enter code here
        
      import 'package:flutter/material.dart';
      
      import 'screen/splashScreen.dart';
      
      class DemoNavigation extends StatefulWidget {
        const DemoNavigation({Key? key}) : super(key: key);
      
         @override
          _DemoNavigationState createState() => _DemoNavigationState();
          }
      
        class _DemoNavigationState extends State<DemoNavigation> {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(),
            body: Container(
             child: Column(
                children: [
                  Text("Navigator Demo"),
                    ElevatedButton(
                        onPressed: () {
                  // here is the function that your are using
                        navigation(context);
                    },
                     child: Text("TO Next Screen"),
                   ),
                 ],
              ),
            ),
         );
      }
      
          /// here you need to change the context that your are passing to build 
           /// copy this   
          void navigation(BuildContext context) {
             Navigator.push(context, MaterialPageRoute(builder: (_) => 
            SplashScreen()));
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-09-12
        • 2021-02-23
        • 2020-09-01
        • 2019-05-16
        • 2019-04-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        • 1970-01-01
        相关资源
        最近更新 更多