I use `vRouter` package for navigation. You can us...
# flutter
m
I use
vRouter
package for navigation. You can use literally any package at this point. Most of them, like
vRouter
, have nav guards. I am giving two of my guards that I set up. These will show how I can use my state notifier providers conveniently. I am sure you will deduce from this that
vRouter
will guard ALL routes before entering and check if I am logged in. If I am not, it woll redirect to the login route. If I am, it'll let me continue. The second guard is for the opposite. If someone types the url to forcefully go to the login page, it will not let them do that.
Copy code
dart
VGuard(
          beforeEnter: (vRedirector) async {
            final authStatus = ref.read(authControllerRiverpod);
            debugPrint('Dashboard VGaurd was called.');
            if (authStatus.status != AuthStatus.authenticated) {
              debugPrint('AuthStatus was: ${authStatus.status.name}');
              vRedirector.to('/login');
            }
          },
          stackedRoutes: [
            myVRoutes['/dashboard']!,
            myVRoutes['/transactions']!,
            myVRoutes['/add']!,
          ],
        ),
        VGuard(
          beforeEnter: (vRedirector) async {
            final authStatus = ref.read(authControllerRiverpod);
            debugPrint('Login VGaurd was called.');
            if (authStatus.status != AuthStatus.unauthenticated) {
              debugPrint('AuthStatus was: ${authStatus.status.name}');
              vRedirector.to(vRedirector.fromUrl!);
            }
          },
          stackedRoutes: [
            myVRoutes['/login']!,
          ],
        ),
Now I am sure that you will find problems or issues here. If you or anyone sees smth that I am doing wrong, plz let me know.