Flutter GetX bindings?

6,473

Solution 1

I have done similar thing hoping this helps 😁

When you are navigating user to HomeScreen after Splash Screen you can navigate using Get.offAll() with Bindings in which you have initialised all your controllers

Navigating to the HomeScreen

Get.offAll(
  () => const InitialScreen(screenTag: appControllersTag),
  binding: InitialScreenBindings(tag: appControllersTag),
)

Initialising all controllers

class InitialScreenBindings implements Bindings {
  String tag;

  InitialScreenBindings({required this.tag});

  @override
  void dependencies() {
    Get.lazyPut(() => BottomNavigationController(), tag: tag);
    Get.lazyPut(() => HomeController(), tag: tag);
    Get.lazyPut(() => OrderController(), tag: tag);
    Get.lazyPut(() => CartController(), tag: tag);
    Get.lazyPut(() => ProfileController(), tag: tag);
  }
}

with this my BottomNavigationController, HomeController, OrderController, CartController and ProfileController is alive in entire lifecycle of app

Quick Suggestion: You can inject controllers with tags, believe me it makes your life easy as it helps in debugging and fixing issues.

Solution 2

you can use the BindingsBuilder callback. Whenever you visit Splash Screen, Getx will create an instance of Splash controller, and as soon as the page will be removed from stack, GetX automatically removes the instance of splash screen controller.

GetPage(
   name: SplashScreen.routeName,
   page: () => const SplashScreen(),
   binding: BindingsBuilder(() {
          Get.lazyPut<SplashController>(
            () => SplashController(),
          );
        }))
Share:
6,473
Sabarish Chungath
Author by

Sabarish Chungath

Updated on January 02, 2023

Comments

  • Sabarish Chungath
    Sabarish Chungath over 1 year

    Iam new to this architecture, I have a doubt regarding the bindings in getX

    I have two controllers cartController and splashController I need this cartController to remain its state as for the entire lifecycle of app splashController only for the splashScreen

    But the splash controller only works when I bind it with other controllers in initial binding in GetMaterialApp

    GetMaterialApp(
          home: const SplashScreen(),
          initialBinding: RootBinding(),
          getPages: [
            GetPage(
              name: SplashScreen.routeName,
              page: () => const SplashScreen(),
            ),
            GetPage(
                name: HomeScreen.routeName,
                page: () => const HomeScreen(),
                children: [
                  GetPage(
                    name: CategoryScreen.routeName,
                    page: () => const CategoryScreen(),
                  ),
                  GetPage(
                    name: AboutScreen.routeName,
                    page: () => const AboutScreen(),
                  ),
                  GetPage(
                    name: CartScreen.routeName,
                    page: () => const CartScreen(),
                  ),
                ]),
          ],
        );
    

    Root binding is

    class RootBinding implements Bindings {
      @override
      void dependencies() {
        Get.put<SplashController>(SplashController());
        Get.put<HomeController>(HomeController());
        Get.put<CategoryController>(CategoryController());
        Get.put<AboutController>(AboutController());
      }
    }
    

    It also doesn't work when i change it to Get.lazyPut()

    I dont know if this is the best practise the above code works but when i remove a controller from initial binding to a page it doesnt work like below

    GetPage(
       name: SplashScreen.routeName,
       page: () => const SplashScreen(),
       binding: SplashBinding()
       ),
    

    link to source code https://github.com/shabhi1997/GetXBaseProject/tree/master