Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter GetX bindings?

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

like image 863
Sabarish Chungath Avatar asked Nov 19 '25 20:11

Sabarish Chungath


1 Answers

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(),
          );
        }))
like image 121
Faiz Shahid Avatar answered Nov 22 '25 09:11

Faiz Shahid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!