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

Flutter:notifyListeners仅主页生效,其他页面无法触发页面切换

问题原因与解决方案

核心原因

  • 在Issue Parts页面执行logout时,虽然loggedIn已设为false且触发了状态更新,但该页面是通过Navigator.push添加到导航栈顶部的。此时Wrapper组件确实会重建并渲染Login页面,但Login页面被压在导航栈下层,用户看到的还是栈顶的Issue Parts页面。
  • 而Forms页面是Wrapper直接返回的子组件,当Wrapper因loggedIn变化重建时,Forms会被直接替换成Login页面,没有导航栈层级的遮挡,所以能正常跳转。

解决方案

在Issue Parts页面的logout操作完成后,将当前页面从导航栈中移除,让下层的Wrapper渲染的Login页面显示出来即可。修改Issue Parts的按钮代码:

ElevatedButton(
  onPressed: () async {
    await globals.logout(); // 设置loggedIn为false并通知状态更新
    Navigator.pop(context); // 关闭当前Issue Parts页面,回到Forms页面(此时Forms会被Wrapper替换为Login)
  },
  child: const Text('LOGOUT'),
),

如果想彻底清空所有历史页面,确保Login页面是导航栈的唯一页面,可以改用pushAndRemoveUntil:

ElevatedButton(
  onPressed: () async {
    await globals.logout();
    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => const Login()),
      (route) => false, // 移除所有之前的路由
    );
  },
  child: const Text('LOGOUT'),
),

内容的提问来源于stack exchange,提问作者TheMortiestMorty

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 01:07:41