简体   繁体   中英

passing Auth data with go_router

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM