android - 如何修复 flutter 异常 : Navigator operation requested with a context that does not include a Navigator

标签 android flutter

我正在尝试使用 flutter 框架创建抽屉导航, 但是我每次运行它都会遇到以下异常

Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.

那么解决方案是什么,有什么帮助吗?

我使用 Navigator 类如下

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

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new AppStates();
  }
}

class AppStates extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return MaterialApp(
      home: new Scaffold(
        appBar: AppBar(
          title: Text("Application App Bar"),
        ),
        drawer: Drawer(
          child: ListView(
            children: <Widget>[
              ListTile(
                title: Text("Next Page"),
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => NextPage()));
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

NextPage类的代码是

class NextPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Next Page App Bar"),
        ),
      ),
    );
  }
}

最佳答案

您似乎没有为当前上下文设置导航器。您应该尝试将 MaterialApp 作为您的根应用程序,而不是使用 StatefulWidget。 MaterialApp 为您管理一个导航器。这是一个如何在 main.dart 中设置应用程序的示例

void main() {
 runApp(MaterialApp(
   title: 'Navigation Basics',
   home: MyApp(),
 ));
}

关于android - 如何修复 flutter 异常 : Navigator operation requested with a context that does not include a Navigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51600412/

相关文章:

android - 终端上的 Gradle 版本与 Project Gradle 版本不同

android-studio - 运行我的第一个 flutter 项目时出错

firebase - 如何从cloud firestore获取数据,其中user.uid等于flutter中的文档ID?

android - Android GridView 中图像 URL 的字符串数组

android - 使用 CameraX 分析时如何选择图像的一部分

flutter - 如何播放 Google 云端硬盘链接中的视频?

flutter - 如何在 onDragCompleted 上从 Draggable 获取位置?

Firebase Google用户未显示在Firebase控制台身份验证中

android - Android 中的压缩需要很长时间

android - 如何让联系人的选定返回只返回选定的联系人而不是全部?