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

GoRouter导航异常:点击无法打开ProfilePage问题求助

GoRouter跳转ProfilePage无反应的问题排查与解决

核心问题:Redirect逻辑强制拦截所有非首页跳转

你的AppRouter中redirect方法存在逻辑问题:当用户处于LoggedInState.loggedIn状态时,无论当前要跳转的目标页面是什么,都会强制返回首页路径。这就导致你点击Profile后,GoRouter刚要执行跳转就被redirect逻辑拉回首页,自然看不到ProfilePage的内容。

// 原错误逻辑
switch (appState.loggedInState) {
  case LoggedInState.loading:
    return splashLocation;
  case LoggedInState.loggedIn:
    return homeLocation; // 强制跳回首页,拦截所有其他页面
}

修复方案:调整Redirect逻辑,仅在必要时跳转

修改redirect方法,仅在加载状态下强制跳转到splash页;已登录状态下,仅当当前处于splash页时才跳转到首页,其他页面正常放行:

redirect: (context, state) {
  final AuthProvider appState =
      Provider.of<AuthProvider>(context, listen: true);

  final homeLocation = AppPage.home.toPath;
  final splashLocation = AppPage.splash.toPath;
  final currentLocation = state.location;

  switch (appState.loggedInState) {
    case LoggedInState.loading:
      // 加载状态下,若不在splash页则跳转过去
      return currentLocation == splashLocation ? null : splashLocation;
    case LoggedInState.loggedIn:
      // 已登录状态下,若当前是splash页则跳转到首页,其他页面放行
      return currentLocation == splashLocation ? homeLocation : null;
  }
}

次要优化:调整抽屉关闭与跳转的顺序

当前代码先执行跳转再关闭抽屉,虽然不是无反应的主因,但调换顺序能提升视觉体验,避免抽屉关闭延迟:

onTap: () {
  Navigator.pop(context); // 先关闭抽屉
  context.goNamed(AppPage.profile.toName); // 再执行跳转
},

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 08:12:37