jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.34k stars 1.62k forks source link

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

Open Ssiswent opened 2 months ago

Ssiswent commented 2 months ago

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 commented 2 months ago

And onPageDispose also run twice more!!!

jonataslaw commented 2 months ago

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.

Ssiswent commented 2 months ago

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?

jonataslaw commented 2 months ago

Yeah, definitely.

Ssiswent commented 2 months ago

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