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

pgpifvop  于 2023-04-07  发布在  Flutter
关注(0)|答案(3)|浏览(208)

如何正确重定向到路由?
我的应用程序必须有一些路由安全性,我试图用GoRouter来做,但它总是执行一个重定向,并总是返回到主页。
只有3个安全路由(配置文件,订单,收藏夹),如果您没有登录,您必须返回登录。
但是当我把3个路由中的任何一个放在url中时,它会将我重定向到登录,并立即重定向到主页(这是不应该发生的事情)
有人能帮帮我吗?我改变了几次逻辑,但我总是回到同一点。

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的最新版本(重定向问题在旧版本上)。
第二步-〉修改重定向逻辑:

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;

注意:我不知道为什么flutter web调试模式不能正常工作。所以我在发布模式下运行项目,瞧,它工作了!
非常感谢您的帮助!!!

enxuqcxy

enxuqcxy1#

initialLocation更改为登录

late final GoRouter _goRouter = GoRouter(
    refreshListenable: GoRouterRefreshStream(sessionCubit.stream),
    // Change initialLocation to login
    initialLocation: APP_PAGE.home.toPath,
kmpatx3s

kmpatx3s2#

只要检查你的用户是否通过了身份验证并且不在登录页面,然后重定向。如果你的用户已经登录,你不需要重定向:

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;
kse8i1jr

kse8i1jr3#

以下链接中的示例可以帮助您解决此问题;
go_router

<pre>
 final GoRouter _router = GoRouter(routes: <RouteBase>[
  GoRoute(
  redirect: (BuildContext context, GoRouterState state) {
    // if (AuthState.of(context).isSignedIn) {
      return '/join_us';
    // } else {
    //   return null;
    // }
  },
  path: '/',
  builder: (BuildContext context, GoRouterState state) {
    return const HomePage();
  },
</pre>

相关问题