使用go_router传递验证数据

发布于 2025-02-06 04:51:17 字数 1680 浏览 1 评论 0原文

在我的项目中,我实现了管理状态的提供者方法,我想与GO Router软件包共享Auth Provister信息,以使用户登录

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (ctx) => Auth(),
          ),
         ListenableProxyProvider<Auth, AppRouter>(
            update: (_, authObj, prevOrders) =>
               AppRouter(authObj)
          ),
}

和在我的Applouter类中,我有一个限制者可以获取auth Data:

class AppRouter with ChangeNotifier {
  final Auth authData;
  AppRouter(this.authData);
  final router = GoRouter(
    initialLocation: '/',
    routes: [

     
      GoRoute(
        name: root,
        path: '/',
        builder: (context, state) => TabsScreen(),
        // redirect: (state) => state.namedLocation(authScreen),
      ),
      GoRoute(
        name: mainScreen,
        path: '/main-screen',
        builder: (context, state) => HomeScreen(),
      ),
      GoRoute(
        name: authscreen,
        path: '/auth-screen',
        builder: (context, state) => AuthScreen(),
      ),

],

    redirect: (state) {
      final loginLoc = state.namedLocation(authScreen);
      final loggingIn = state.subloc == loginLoc;

  var loggedIn = authData.isLoggedIn;
 if (!loggedIn && !loggingIn) return loginLoc;
  if (loggedIn && (loggingIn)) return root;

      return null;
    },

但是我可以't访问我的班级中的authdata,我会收到此错误:

The instance member 'authData' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression

In my project, I implement the Provider method to manage state, and I'd like to share auth provider info with the go router package to keep users logged in

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider(
            create: (ctx) => Auth(),
          ),
         ListenableProxyProvider<Auth, AppRouter>(
            update: (_, authObj, prevOrders) =>
               AppRouter(authObj)
          ),
}

and within my AppRouter class I have a constructer to get auth data :

class AppRouter with ChangeNotifier {
  final Auth authData;
  AppRouter(this.authData);
  final router = GoRouter(
    initialLocation: '/',
    routes: [

     
      GoRoute(
        name: root,
        path: '/',
        builder: (context, state) => TabsScreen(),
        // redirect: (state) => state.namedLocation(authScreen),
      ),
      GoRoute(
        name: mainScreen,
        path: '/main-screen',
        builder: (context, state) => HomeScreen(),
      ),
      GoRoute(
        name: authscreen,
        path: '/auth-screen',
        builder: (context, state) => AuthScreen(),
      ),

],

    redirect: (state) {
      final loginLoc = state.namedLocation(authScreen);
      final loggingIn = state.subloc == loginLoc;

  var loggedIn = authData.isLoggedIn;
 if (!loggedIn && !loggingIn) return loginLoc;
  if (loggedIn && (loggingIn)) return root;

      return null;
    },

however I can't access authData within my class and I get this error :

The instance member 'authData' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2025-02-13 04:51:17

本文帮助我解决了问题,还有有关使用go_router实施导航2.0的完整指南:

flutter导航器2.0:使用go_router

this article helped me solve my problem and Also there is complete guide about implementing Navigation 2.0 using Go_Router :

Flutter Navigator 2.0: Using go_router

走走停停 2025-02-13 04:51:17

这是正常的。即使您在想:“当我将我的authdata分配给DART时,重定向也会发生。对于DART,您正在尝试在创建路​​由器的时刻使用Authdata的值,因此未分配它。例如,如果Authdata是一个静态字段,并且已经分配了,则可以执行此操作。

您可能可以通过阅读此书来实现您要实现的目标:
https://gorouter.dev/parameters

编辑:
这是我要做的代码段:

final router = GoRouter(
    initialLocation: '/',
    routes: [
      GoRoute(
        name: root,
        path: '/',
        builder: (context, state) => TabsScreen(),
      ),
      GoRoute(
        name: mainScreen,
        path: '/main-screen',
        builder: (context, state) => HomeScreen(),
      ),
      GoRoute(
        name: authscreen,
        path: '/auth-screen',
        builder: (context, state) => AuthScreen(),
      ),

    ],

    redirect: (state) {
      final Auth data=state.extra! as Auth;
      final loginLoc = state.namedLocation(authScreen);
      final loggingIn = state.subloc == loginLoc;

      var loggedIn = data.isLoggedIn;
      if (!loggedIn && !loggingIn) return loginLoc;
      if (loggedIn && loggingIn) return root;

      return null;
    },
);

注意:记住要始终通过这样的状态:

context.go('/route', extra:authData);

This is normal. Even if you're thinking:"The redirect will happen when I'll have my authData assigned" Dart don't see it like this. For dart, you're trying to use the value of authData in the moment of the creation of the router, so, it is not assigned. For example, you could do this if authData was a static field and it was already assigned.

You can probably achieve what you're trying to achieve by reading this:
https://gorouter.dev/parameters

EDIT:
Here is a code snippet of what i would do:

final router = GoRouter(
    initialLocation: '/',
    routes: [
      GoRoute(
        name: root,
        path: '/',
        builder: (context, state) => TabsScreen(),
      ),
      GoRoute(
        name: mainScreen,
        path: '/main-screen',
        builder: (context, state) => HomeScreen(),
      ),
      GoRoute(
        name: authscreen,
        path: '/auth-screen',
        builder: (context, state) => AuthScreen(),
      ),

    ],

    redirect: (state) {
      final Auth data=state.extra! as Auth;
      final loginLoc = state.namedLocation(authScreen);
      final loggingIn = state.subloc == loginLoc;

      var loggedIn = data.isLoggedIn;
      if (!loggedIn && !loggingIn) return loginLoc;
      if (loggedIn && loggingIn) return root;

      return null;
    },
);

Note: Remember to always pass the state like this:

context.go('/route', extra:authData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文