简体   繁体   English

处理手势时抛出以下断言:使用不包含导航器的上下文请求导航器操作

[英]The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator

I declared a class, returned MaterialApp and used button in it also used Navigator.Push method for navigation to different page, but it gave exception我声明了一个 class,返回了 MaterialApp 并在其中使用了按钮,还使用了 Navigator.Push 方法导航到不同的页面,但它给出了异常

Navigator operation requested with a context that does not include a Navigator使用不包含导航器的上下文请求的导航器操作

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
            SafeArea(
            child: Scaffold(
            backgroundColor: Colors.red[100],
            body:Column(
              children: [
                   SizedBox(height: 50,),
                  Align(
                    alignment:Alignment.center,
                     child:Image.asset("assets/icons/appicon.png",
                   height:150),
                   ),
                   
                   Align(
                     alignment: Alignment.bottomCenter,
                     child: RaisedButton(  
  
                             child: Text('Click Picture'),  
  
                             color: Colors.red[800],  
  
                               onPressed: () {
  
                                            Navigator.push(
  
                                            context,
  
                                            MaterialPageRoute(builder: (context) =>  CameraRoute()
                                            ),
                                            );
  
                               },  
  
                             
  
                     ),
                   )
              ],
             
          ),
              
            ),
                        
        )
);
  

    //throw UnimplementedError();
  }
}

Separate the page/screen from MyApp by creating a new widget.通过创建一个新的小部件将页面/屏幕与MyApp分开。

Like so像这样

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Column(
            children: [
              SizedBox(height: 100),
              Image.asset(
                "assets/icons.appicon.png",
                height: 150,
              ),
              RaisedButton(
                child: Text("Click Picture"),
                color: Colors.red,
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => CameraRoute(),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

You're trying to get Navigator from a context that doesn't have a Navigator as parent.您正在尝试从没有 Navigator 作为父级的上下文中获取 Navigator。 The only navigator in your code is in the MaterialApp, but that is a child of context.您代码中唯一的导航器在 MaterialApp 中,但它是上下文的子项。

The minimal way of getting a context that is a child of MaterialApp is to wrap your Scaffold with a Builder.获取作为 MaterialApp 子项的上下文的最小方法是用构建器包装您的脚手架。

A better approach would be to split your code into more widgets (the build-method of each widget exposes a context) as mentioned by the other answer.更好的方法是将您的代码拆分为更多的小部件(每个小部件的构建方法公开一个上下文),如其他答案所述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator 使用不包含导航器的上下文请求的导航器操作 - Navigator operation requested with a context that does not include a Navigator 实施导航器时发生异常。 使用不包含导航器的上下文请求的导航器操作 - Exception while implementing navigator. 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 - Flutter 为什么“使用不包含导航器的上下文请求导航器操作” - WHY "Navigator operation requested with a context that does not include a Navigator" 错误:使用不包含导航器的上下文请求导航器操作 - Error: 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 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter) - Navigator operation requested with a context that does not include a Navigator.(Flutter) 使用不包含 Navigator (Flutter) 的上下文请求的 Navigator 操作 - Navigator operation requested with a context that does not include a Navigator (Flutter) 异常:使用不包含导航器的上下文请求的导航器操作 - Exception : Navigator operation requested with a context that does not include a Navigator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM