getx icon indicating copy to clipboard operation
getx copied to clipboard

GetX 5 `GetMiddleware` almost all functions like `onPageBuilt` run twice

Open Ssiswent opened this issue 1 year ago • 5 comments

With nested navigation in example_nav2 example project, they will all run twice more.

Codes:

class AppPages {
  AppPages._();

  static const initial = Routes.home;

  static final routes = [
    GetPage(
      middlewares: [MainMiddleware()],
      preventDuplicates: true,
      name: _Paths.home,
      page: () => const HomeView(),
      bindings: [
        HomeBinding(),
      ],
      title: null,
      participatesInRootNavigator: true,
      children: [
        GetPage(
          name: _Paths.dashboard,
          page: () => const DashboardView(),
          bindings: [
            DashboardBinding(),
          ],
        ),
        GetPage(
          // middlewares: [
          //   //only enter this route when authed
          //   EnsureAuthMiddleware(),
          // ],
          name: _Paths.profile,
          page: () => const ProfileView(),
          title: 'Profile',
          transition: Transition.size,
          bindings: [ProfileBinding()],
        ),
        GetPage(
          name: _Paths.products,
          page: () => const ProductsView(),
          title: 'Products',
          transition: Transition.cupertino,
          showCupertinoParallax: true,
          // participatesInRootNavigator: true,
          bindings: [ProductsBinding(), ProductDetailsBinding()],
          children: [
            GetPage(
              name: _Paths.settings,
              page: () => const SettingsView(),
              participatesInRootNavigator: true,
              bindings: [
                SettingsBinding(),
              ],
            ),
            GetPage(
              name: _Paths.productDetails,
              transition: Transition.cupertino,
              showCupertinoParallax: true,
              page: () => const ProductDetailsView(),
              participatesInRootNavigator: true,
              bindings: const [],
              // middlewares: [
              //   //only enter this route when authed
              //   EnsureAuthMiddleware(),
              // ],
            ),
          ],
        ),
      ],
    ),
    GetPage(
      name: _Paths.settings,
      page: () => const SettingsView(),
      bindings: [
        SettingsBinding(),
      ],
    ),
  ];
}

class MainMiddleware extends GetMiddleware {
  @override
  void onPageDispose() {
    log('MainMiddleware onPageDispose');
    super.onPageDispose();
  }

  @override
  Widget onPageBuilt(Widget page) {
    log('MainMiddleware onPageBuilt');
    return super.onPageBuilt(page);
  }

  @override
  GetPage? onPageCalled(GetPage? page) {
    log('MainMiddleware onPageCalled');
    return super.onPageCalled(page);
  }

  @override
  List<R>? onBindingsStart<R>(List<R>? bindings) {
    log('MainMiddleware onBindingsStart');
    return super.onBindingsStart(bindings);
  }

  @override
  GetPageBuilder? onPageBuildStart(GetPageBuilder? page) {
    log('MainMiddleware onPageBuildStart');
    return super.onPageBuildStart(page);
  }
}

Screenshots: image

Ssiswent avatar Aug 14 '24 09:08 Ssiswent

And onPageDispose also run twice more!!!

Ssiswent avatar Aug 15 '24 01:08 Ssiswent

This was introduced by Nested Bindings. When someone directly accesses a nested route in the browser, the parent's dependencies and middleware must be loaded. However, you're right, it should only start once, so I'm going to change the api to initialize Bindings only once.

jonataslaw avatar Aug 16 '24 13:08 jonataslaw

This was introduced by Nested Bindings. When someone directly accesses a nested route in the browser, the parent's dependencies and middleware must be loaded.

However, you're right, it should only start once, so I'm going to change the api to initialize Bindings only once.

Thanks, so you mean that mb next version of GetX will change this?

Ssiswent avatar Aug 16 '24 13:08 Ssiswent

Yeah, definitely.

jonataslaw avatar Aug 16 '24 13:08 jonataslaw

Yeah, definitely.

Thanks for your help! Btw, could you please help with this issuse? Or mb there is something wrong with my nested routes. https://github.com/jonataslaw/getx/issues/3175

Ssiswent avatar Aug 16 '24 13:08 Ssiswent