1

I have been experimenting with GoRouter and ShellRouter. With the current configuration apart from the redirect to the Login page after a successful Auth has been performed no other navigation works. I have tried to add the routerConfig to the MaterialApp but still the same results. Also I get no errors across the application. Been trying to understand the ShellRouter and I have a feeling its related with how I set those keys?

-- update --

After further investigation it seems that everytime you go or push a new route the entire GoRouter gets rebuild and with this redirect gets triggered again. Due to the fact that GoRouter does not have any proper Guards features redirect needs to be handled properly.

====================

MaterialApp.router(
                  debugShowCheckedModeBanner: false,
                  title: 'iHub',
                  theme: theme.light(settings.value.sourceColor),
                  darkTheme: theme.dark(settings.value.sourceColor),
                  themeMode: theme.themeMode(),
                  routerDelegate: appRouter.routerDelegate,
                  routeInformationProvider: appRouter.routeInformationProvider,
                  routeInformationParser: appRouter.routeInformationParser,
                ),
              );

========================

final _parentKey = GlobalKey<NavigatorState>();
final _shellKey = GlobalKey<NavigatorState>();

final appRouter = GoRouter(
  initialLocation: Routes.login,
  redirect: (BuildContext context, GoRouterState state) {
    final bool isAuthenticated =
        context.read<AuthenticationBloc>().isAuthenticated;
    if (!isAuthenticated) {
      return Routes.login;
    } else {
      return Routes.dashboard;
    }
  },
  navigatorKey: _parentKey,
  routes: [
    ShellRoute(
      // * => shell route for the navigation bar
      navigatorKey: _shellKey,
      builder: (context, state, child) {
        return RootLayout(child: child);
      },
      routes: [
        GoRoute(
          name: 'dashboard',
          path: Routes.dashboard,
          pageBuilder: (context, state) => NoTransitionPage(
            key: state.pageKey,
            child: const DashboardPage(),
          ),
        ),
        GoRoute(
          name: 'documents',
          path: Routes.documentsList,
          pageBuilder: (context, state) => NoTransitionPage(
            key: state.pageKey,
            child: const DocumentsPage(),
          ),
        ),
        GoRoute(
          name: 'websites',
          path: Routes.documentsWebsitesList,
          pageBuilder: (context, state) => NoTransitionPage(
            key: state.pageKey,
            child: const WebsitesPage(),
          ),
        ),
        GoRoute(
          name: 'tasks',
          path: Routes.tasksList,
          pageBuilder: (context, state) => NoTransitionPage(
            key: state.pageKey,
            child: const ListTasksPage(),
          ),
        ),
      ],
    ),
    // * => Auth
    GoRoute(
      name: 'login',
      parentNavigatorKey: _parentKey,
      path: Routes.login,
      pageBuilder: (context, state) => const NoTransitionPage(
        key: ValueKey('login'),
        child: LoginPage(),
      ),
    ),
    GoRoute(
      name: 'register',
      parentNavigatorKey: _parentKey,
      path: Routes.register,
      pageBuilder: (context, state) => const NoTransitionPage(
        key: ValueKey('register'),
        child: RegisterPage(),
      ),
    ),
    // * => documents
    GoRoute(
      name: 'addDocument',
      parentNavigatorKey: _parentKey,
      path: Routes.addDocument,
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const AddDocumentPage(),
      ),
    ),
    // * => websites
    GoRoute(
      name: 'addWebsite',
      parentNavigatorKey: _parentKey,
      path: Routes.addWebsite,
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const AddWebsitePage(),
      ),
    ),
    // * => tasks
    GoRoute(
      name: 'addTask',
      parentNavigatorKey: _parentKey,
      path: Routes.addTask,
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const AddTaskPage(),
      ),
    ),
    // * => search
    GoRoute(
      name: 'search',
      parentNavigatorKey: _parentKey,
      path: Routes.search,
      pageBuilder: (context, state) => NoTransitionPage(
        key: state.pageKey,
        child: const SearchPage(),
      ),
    ),
  ],
);
Adrian0012
  • 175
  • 1
  • 5

0 Answers0