如何使用 GoRouter 正确重定向到路由?

shr*_*t91 6 session redirect dart flutter flutter-go-router

我怎样才能正确重定向到路线?

\n

我的应用程序必须具有一定的路由安全性,我尝试使用 GoRouter\n 来做到这一点,但它总是执行一次重定向并总是返回到主页。

\n

只有 3 条安全路径(个人资料、订单、收藏夹),如果您尚未登录,则必须返回登录。

\n

但是当我将 3 个路由中的任何一个放入 url 时,它会将我重定向到登录名并立即到主页(这是不应该发生的情况)

\n

有人可以帮助我吗?\n我多次改变了逻辑,但我总是回到同一点。

\n
class AppRouter {\n  final SessionCubit sessionCubit;\n\n  GoRouter get router => _goRouter;\n\n  AppRouter(this.sessionCubit);\n\n  late final GoRouter _goRouter = GoRouter(\n    refreshListenable: GoRouterRefreshStream(sessionCubit.stream),\n    initialLocation: APP_PAGE.home.toPath,\n    routes: <GoRoute>[\n      GoRoute(\n        path: APP_PAGE.home.toPath,\n        name: APP_PAGE.home.toName,\n        builder: (context, state) => MyHomePage(),\n      ),\n      GoRoute(\n        path: APP_PAGE.login.toPath,\n        name: APP_PAGE.login.toName,\n        builder: (context, state) => const LoginPage(),\n      ),\n      GoRoute(\n        path: APP_PAGE.profile.toPath,\n        name: APP_PAGE.profile.toName,\n        builder: (context, state) => MyProfile(),\n      ),\n      GoRoute(\n        path: APP_PAGE.page1.toPath,\n        name: APP_PAGE.page1.toName,\n        builder: (context, state) => const Page1(),\n      ),\n      GoRoute(\n        path: APP_PAGE.error.toPath,\n        name: APP_PAGE.error.toName,\n        builder: (context, state) => ErrorPage(error: state.extra.toString()),\n      ),\n      GoRoute(\n        path: APP_PAGE.page2.toPath,\n        name: APP_PAGE.page2.toName,\n        builder: (context, state) => const Page2(),\n      ),        \n    ],\n    debugLogDiagnostics: true,\n    errorBuilder: (context, state) => ErrorPage(error: state.error.toString()),\n    redirect: (context, state) {\n  \n      final userAutheticated = sessionCubit.state is Authenticated;\n      if (userAutheticated) {\n        if (state.subloc == '/login') return '/home';\n        if (state.subloc == '/profile') return '/profile';\n        if (state.subloc.contains('/page1')) return '/page1';\n        return '/home';\n      } else {\n        if (state.subloc == '/home') {\n          return '/home';\n        }\n        if (state.subloc == '/page2') {\n          return '/page2';\n        } else {\n          return '/login';\n        }    \n       \n      }\n    },\n  );\n}\n
Run Code Online (Sandbox Code Playgroud)\n

更新 05/11 \n我找到了解决方案。

\n

首先-> 使用 go_router 5.1.5 的最新版本(重定向问题出现在旧版本上)。

\n

第二->我修改重定向逻辑:

\n
if(!userAutheticated && !onloginPage && !onpublicPage1 && !onPublicPage2 && !onPublicPageN){\n        return '/login';\n      }\n      if (userAutheticated && onloginPage) {\n        return '/home';\n      }\n      //you must include this. so if condition not meet, there is no redirect\n      return null;\n
Run Code Online (Sandbox Code Playgroud)\n

注意:我不知道为什么flutter web debug模式不能正常工作。所以我在发布模式下运行该项目,Voil\xc3\xa1,它可以工作!

\n

非常感谢你的帮助!!!

\n

its*_*mud 0

更改initialLocation为登录

  late final GoRouter _goRouter = GoRouter(
    refreshListenable: GoRouterRefreshStream(sessionCubit.stream),
    // Change initialLocation to login
    initialLocation: APP_PAGE.home.toPath,
Run Code Online (Sandbox Code Playgroud)