简体   繁体   English

如何修复使用不包含导航器的上下文请求的导航器操作。 ' 在 flutter

[英]How to fix 'Navigator operation requested with a context that does not include a Navigator. ' in flutter

I encounter this error.我遇到这个错误。

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

I follow the guidelines on the Internet but i'm still confused on how to fix this error.我遵循互联网上的指南,但我仍然对如何修复此错误感到困惑。 Here is my code这是我的代码


     class SecondScreen extends StatelessWidget{
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [Colors.amberAccent, Colors.red]),
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,// add Column
                children: <Widget>[
                  Text('Welcome', style: TextStyle( // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)
                  ),
                  RaisedButton(onPressed: () {
                    Navigator.pop(context);
                  },
                    child: Text('Button'),
                    shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)
                    ),
                    color: Colors.white,
                    splashColor: Colors.blue,
                    textColor: Color(0xfffe67e22),
                  ), // your button beneath text
                ],
              ),
            ),
          ),
        );
      }
    }
    ```

The problem is not in your second page.问题不在您的第二页。 Actually the problem is in your main.dart ;实际上问题出在你的main.dart You should create a new widget for home property of MaterialApp instead of using a Scaffold widget directly;您应该为MaterialApp的 home 属性创建一个新的小部件,而不是直接使用Scaffold小部件;

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: App(),
    ),
  );
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () => Navigator.push(
                context,
                MaterialPageRoute(builder: (_) => SecondPage()),
              ),
              child: Text("SecondPage"),
            ),
          ],
        ),
      ),
    );
  }
}

Screenshot:截屏:

在此处输入图像描述


Full code:完整代码:

void main() => runApp(MaterialApp(home: MyApp(), debugShowCheckedModeBanner: false));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // add Column
            children: <Widget>[
              Text('Welcome',
                  style: TextStyle(
                      // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)),
              RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute<Null>(builder: (BuildContext context) {
                    return SecondScreen();
                  }));
                },
                child: Text('Button'),
                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
                color: Colors.white,
                splashColor: Colors.blue,
                textColor: Color(0xfffe67e22),
              ), // your button beneath text
            ],
          ),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topLeft, end: Alignment.bottomRight, colors: [Colors.amberAccent, Colors.red]),
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center, // add Column
            children: <Widget>[
              Text('Welcome',
                  style: TextStyle(
                      // your text
                      fontSize: 50.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.white)),
              RaisedButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text('Button'),
                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
                color: Colors.white,
                splashColor: Colors.blue,
                textColor: Color(0xfffe67e22),
              ), // your button beneath text
            ],
          ),
        ),
      ),
    );
  }
}

暂无
暂无

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

相关问题 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter) - Navigator operation requested with a context that does not include a Navigator.(Flutter) Flutter 使用不包括导航器的上下文请求导航器操作。 在启动画面上 - Flutter Navigator operation requested with a context that does not include a Navigator. on splash screen Flutter 闪屏错误 - 使用不包含导航器的上下文请求导航器操作。 我该如何解决这个错误 - Flutter splash screen error - Navigator operation requested with a context that does not include a Navigator. How can I solve this error 实施导航器时发生异常。 使用不包含导航器的上下文请求的导航器操作 - Exception while implementing navigator. Navigator operation requested with a context that does not include a Navigator 如何修复颤振异常:使用不包含导航器的上下文请求导航器操作 - how to fix flutter exception : Navigator operation requested with a context that does not include a Navigator 在从内存中恢复应用程序时显示密码屏幕 [使用不包含导航器的上下文请求的导航器操作。] - show pincode screen on app resume from memory [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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM