getx icon indicating copy to clipboard operation
getx copied to clipboard

The middleware will not be triggered if I use nested navigation

Open Coder7777 opened this issue 2 years ago • 2 comments

I refer to official documentation to implement the nested navigation.

https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md#nested-navigation

For nested navigation, the GetX works fine.

But the middleware will not take effect in nested navigation

Coder7777 avatar Oct 17 '23 20:10 Coder7777

class AuthorizedRouterGuard extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    var tokenCtrl = Get.find<TokenController>();
    return JwtDecoder.isExpired(tokenCtrl.accessToken!)
        ? const RouteSettings(name: AppRoutes.password)
        : null;
  }
}
    Navigator(
        key: Get.nestedKey(1),
        initialRoute: AppRoutes.home,
        onGenerateRoute: (settings) {
          if (settings.name == "/home") {
            return GetPageRoute(
              settings: settings,
              page: () => const HomePage(),
              middlewares: [AuthorizedRouterGuard()],
              transition: Transition.topLevel,
            );
          } else if (settings.name == "/store") {
            return GetPageRoute(
              settings: settings,
              page: () => const StoresPage(),
              middlewares: [AuthorizedRouterGuard()],
              transition: Transition.topLevel,
            );
          } else if (settings.name == "/catalogues") {
            return GetPageRoute(
              settings: settings,
              page: () => const CatalogueAllCategoriesPage(),
              middlewares: [AuthorizedRouterGuard()],
              transition: Transition.topLevel,
            );
          } else if (settings.name == "/me") {
            return GetPageRoute(
              settings: settings,
              page: () => const MePage(),
              middlewares: [AuthorizedRouterGuard()],
              transition: Transition.topLevel,
            );
          }
          return null;
        },
 et.toNamed(tabRoutes[index], id: 1);

Nested navigation can work, but AuthorizedRouterGuard will not be triggered.

Unless remove 'Id' in Get.toNamed like below

Get.toNamed(tabRoutes[index]);

But this is not what I want, I will be a normal navigation, popup the page as normal, not nested navigation.

Coder7777 avatar Oct 17 '23 20:10 Coder7777

did you find any solution to this?

adekmaulana avatar Jun 10 '24 13:06 adekmaulana