How can i use getx bindings?

I have flutter app and I have many controller in my app when I use one controller in other controller

so one people suggested me to use bindings but when I use binging and use get.put method It says me controller not initialised can anyone suggest me how to use bidding in flutter


Solution 1:

Create a class and implements Binding

class HomeBinding implements Bindings {}

Your IDE will automatically ask you to override the "dependencies" method, and you just need to click on the lamp, override the method, and insert all the classes you are going to use on that route:

class HomeBinding implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<HomeController>(() => HomeController());
    Get.put<Service>(()=> Api());
  }
}

class DetailsBinding implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<DetailsController>(() => DetailsController());
  }
}

Now you just need to inform your route, that you will use that binding to make the connection between route manager, dependencies and states.

Using named routes:

getPages: [
  GetPage(
    name: '/',
    page: () => HomeView(),
    binding: HomeBinding(),
  ),
  GetPage(
    name: '/details',
    page: () => DetailsView(),
    binding: DetailsBinding(),
  ),
];

Using normal routes:

Get.to(Home(), binding: HomeBinding());
Get.to(DetailsView(), binding: DetailsBinding())

There, you don't have to worry about memory management of your application anymore, Get will do it for you.

The Binding class is called when a route is called, you can create an "initialBinding in your GetMaterialApp to insert all the dependencies that will be created.

GetMaterialApp(
  initialBinding: SampleBind(),
  home: Home(),
);

BindingsBuilder

The default way of creating a binding is by creating a class that implements Bindings. But alternatively, you can use BindingsBuilder callback so that you can simply use a function to instantiate whatever you desire.

Example:

getPages: [
  GetPage(
    name: '/',
    page: () => HomeView(),
    binding: BindingsBuilder(() {
      Get.lazyPut<ControllerX>(() => ControllerX());
      Get.put<Service>(()=> Api());
    }),
  ),
  GetPage(
    name: '/details',
    page: () => DetailsView(),
    binding: BindingsBuilder(() {
      Get.lazyPut<DetailsController>(() => DetailsController());
    }),
  ),
];

That way you can avoid to create one Binding class for each route making this even simpler.

Both ways of doing work perfectly fine and we want you to use what most suit your tastes.

SmartManagement GetX by default disposes unused controllers from memory, even if a failure occurs and a widget that uses it is not properly disposed. This is what is called the full mode of dependency management. But if you want to change the way GetX controls the disposal of classes, you have SmartManagement class that you can set different behaviors.

How to change

If you want to change this config (which you usually don't need) this is the way:

void main () {
  runApp(
    GetMaterialApp(
      smartManagement: SmartManagement.onlyBuilders //here
      home: Home(),
    )
  )
}

SmartManagement.full It is the default one. Dispose classes that are not being used and were not set to be permanent. In the majority of the cases you will want to keep this config untouched. If you new to GetX then don't change this.

SmartManagement.onlyBuilders With this option, only controllers started in init: or loaded into a Binding with Get.lazyPut() will be disposed.

If you use Get.put() or Get.putAsync() or any other approach, SmartManagement will not have permissions to exclude this dependency.

With the default behavior, even widgets instantiated with "Get.put" will be removed, unlike SmartManagement.onlyBuilders.

SmartManagement.keepFactory Just like SmartManagement.full, it will remove it's dependencies when it's not being used anymore. However, it will keep their factory, which means it will recreate the dependency if you need that instance again.