You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter报错:Navigator操作的Context不包含Navigator的解决求助

Flutter Navigator 错误解决:"Navigator operation requested with a context that does not include a Navigator"

作为Flutter新手,尝试跳转到Registro页面时碰到这个错误:Navigator operation requested with a context that does not include a Navigator,试过两个Stack Overflow的解决方案但没用,我的代码如下:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(home: Login());
  }
}

class Login extends StatelessWidget {
  const Login({Key? key}) : super(key: key);
  static const String _title = 'Sample App';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
        bottomNavigationBar: BottomAppBar(
          elevation: 3,
          child: Row(
            children: <Widget>[
              const Text('¿No tienes una cuenta?'),
              TextButton(
                child: const Text(
                  'Registrate',
                  style: TextStyle(fontSize: 20),
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => new Registro()),
                  );
                },
              )
            ],
            mainAxisAlignment: MainAxisAlignment.center,
          ),
        ),
      ),
    );
  }
}

问题根源

你的代码里重复创建了两个MaterialApp:根组件MyApp返回了一个MaterialApp,而Login组件又自己创建了另一个MaterialApp。Navigator是MaterialApp自带的导航器,你在Login的context里调用Navigator时,这个context属于外层MaterialApp的子节点,但内层的MaterialApp会重新构建导航上下文,导致当前context找不到对应的Navigator实例。

解决方法

最直接的办法是移除Login里的MaterialApp,直接返回Scaffold即可——因为根组件MyApp已经提供了全局的MaterialApp,不需要重复创建。修改后的Login代码:

class Login extends StatelessWidget {
  const Login({Key? key}) : super(key: key);
  static const String _title = 'Sample App';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text(_title)),
      body: const MyStatefulWidget(),
      bottomNavigationBar: BottomAppBar(
        elevation: 3,
        child: Row(
          children: <Widget>[
            const Text('¿No tienes una cuenta?'),
            TextButton(
              child: const Text(
                'Registrate',
                style: TextStyle(fontSize: 20),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => new Registro()),
                );
              },
            )
          ],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
      ),
    );
  }
}

如果确实需要在Login里保留MaterialApp(比如单独配置路由),可以给内层MaterialApp添加GlobalKey<NavigatorState>,或者用Builder组件包裹按钮获取正确的context,但方案1是最简洁且符合常规项目结构的做法。

之前你尝试的方案没生效,是因为那些方案针对的是context不在Navigator子树里的情况,但你的核心问题是重复创建MaterialApp导致的上下文层级混乱,所以没命中关键点。

内容的提问来源于stack exchange,提问作者Michael Adolfo Huerta Ramirez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:55:15