首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GoRouter正确地重定向到路由?

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

Stack Overflow用户
提问于 2022-11-04 23:25:42
回答 2查看 197关注 0票数 0

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

我的应用程序必须有一些路由安全性,我尝试用GoRouter来实现它,但是它总是再执行一次重定向,并且总是返回到家。

只有3条安全路线(配置文件,订单,收藏夹),如果你没有登录,你必须返回登录。

但是当我将三条路线中的任何一条放在url中时,它会将我重定向到登录,并立即返回到家中(这是不应该发生的事情)。

有人能帮我吗?我几次改变了逻辑,但我总是回到同样的观点。

代码语言:javascript
复制
class AppRouter {
  final SessionCubit sessionCubit;

  GoRouter get router => _goRouter;

  AppRouter(this.sessionCubit);

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

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

First ->使用go_router 5.1.5的最后一个版本(重定向问题在旧版本上)。

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

代码语言:javascript
复制
if(!userAutheticated && !onloginPage && !onpublicPage1 && !onPublicPage2 && !onPublicPageN){
        return '/login';
      }
      if (userAutheticated && onloginPage) {
        return '/home';
      }
      //you must include this. so if condition not meet, there is no redirect
      return null;

注意:我不知道为什么颤振web调试模式不能正常工作。因此,我运行的项目发布模式和视频,这是可行的!

非常感谢你的帮助!

EN

回答 2

Stack Overflow用户

发布于 2022-11-05 02:50:44

initialLocation更改为登录

代码语言:javascript
复制
  late final GoRouter _goRouter = GoRouter(
    refreshListenable: GoRouterRefreshStream(sessionCubit.stream),
    // Change initialLocation to login
    initialLocation: APP_PAGE.home.toPath,
票数 0
EN

Stack Overflow用户

发布于 2022-11-05 05:07:49

只需检查您的用户是否经过身份验证,而不是在登录页面中,然后重定向。如果用户已经登录,则不需要重定向:

代码语言:javascript
复制
    final bool userAutheticated = sessionCubit.state is Authenticated;

    final bool onloginPage = state.subloc == '/login';
    

    if(!userAutheticated && !onloginPage){
      return '/login';
    }
    if(userAutheticated && onloginPage){
      return 'home';
    }
    //you must include this. so if condition not meet, there is no redirect
    return null;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74323919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档