Flutter web : getx middleware and route arguments (GetX example_nav2)

125

You are creating a new GetNavConfig using only the location. You should just return the existing GetNavConfig from the parameter:

if (role == "seller") {
    return route;
  }
Share:
125
AkMax
Author by

AkMax

Updated on January 04, 2023

Comments

  • AkMax
    AkMax over 1 year
    1. Getx example_nav2 https://github.com/jonataslaw/getx/tree/master/example_nav2

    2. my app_pages.dart

                GetPage(
                preventDuplicates: true,
                name: _Paths.sRoot,
                page: () => const SellerDashBoardRootView(),
                binding: SellerDashBoardRootViewBinding(),
                middlewares: [
                  //only enter this route when user is a seller
                  EnsureAuthSeller()
                ],
                children: [
    
                  GetPage(
                    name: _Paths.sellerDashboard,
                    page: () => const SellerDashboardScreen(),
                    binding: SellerDashboardBinding(),
                    transition: Transition.fadeIn),
    
                  GetPage(
                    preventDuplicates: true,
                    name: _Paths.sProductsRoot,
                    page: () => const SellerProductsRootView(),
                    binding: SellerProductsRootViewBinding(),
                    transition: Transition.fadeIn,
                  ),
    
                  GetPage(
                    name: _Paths.sellerProducts,
                    page: () => const SellerProductsScreen(),
                    binding: SellerProductsBinding(),
                    transition: Transition.fadeIn,
                    children: [
    
                      GetPage(
                      name: _Paths.sellerAddProduct,
                      page: () => const SellerAddProductScreen(),
                      binding: SellerAddProductBinding(),
                      transition: Transition.fadeIn),
    
                      GetPage(
                      name: _Paths.sellerStock,
                      page: () => const SellerStockScreen(),
                      binding: SellerStockBinding(),
                      transition: Transition.fadeIn),
    
                     ],
                   ),
                 ]
               )
    
    1. EnsureAuthSeller (GetMiddleware)
    class EnsureAuthSeller extends GetMiddleware {
      @override
      Future<GetNavConfig?> redirectDelegate(GetNavConfig route) async {
        if (!AuthService().isLogin) {
          return null;
        } else {
          var role = await LocalStorageServices().getUserRole(keyRole);
          if (role != "seller") {
            return null;
          }
          if (role == "seller") {
            return GetNavConfig.fromRoute(route.location!);
          }
        }
        return await super.redirectDelegate(route);
      }
    }
    
    1. Passing arguments from Routes.sellerProducts to Routes.sellerAddProduct

    Routes.sellerProducts :

    Map<String, String> productDetails = {"productInfos":"test"};
    
    Get.rootDelegate.toNamed(Routes.sellerAddProduct, arguments: productDetails);
    

    Routes.sellerAddProduct :

    final productDetails = Get.rootDelegate.arguments();
    print("result = $productDetails");
    
    1. Result

    Without EnsureAuthSeller (GetMiddleware) => result = {"productInfos":"test"}

    With EnsureAuthSeller (GetMiddleware) => result = null

    Why ? and what is the best way ?