Flutter GetX Bindings not deleting Controller properly

I use GetMaterialApp and getPages a list with GetPage. I cannot grasp how some controllers get deleted and initialized. One of the pages, which is the first one I am implementing this on, is this page:

GetPage(
    name: Routes.registration,
    page: () => const RegistrationScreen(),
    binding: RegistrationBindings(),
  ),

Where my binding is following:

class RegistrationBindings implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<ParticipantController>(() => ParticipantController());
    Get.lazyPut<GroupController>(() => GroupController());
    Get.lazyPut<RegistrationController>(() => RegistrationController());
  }
}

The RegistrationScreen is a StatelessWidget and uses AppBar.

When I click to registration view via this button:

NavigateButton(
  text: 'Registration',
  icon: Icons.group,
  onPressed: () => Get.toNamed(Routes.registration)
),

The controllers are being loaded:

[GETX] GOING TO ROUTE /registration
[GETX] Instance "RegistrationController" has been created
[GETX] Instance "RegistrationController" has been initialized
[GETX] Instance "ParticipantController" has been created
[GETX] Instance "ParticipantController" has been initialized
[GETX] Instance "GroupController" has been created
[GETX] Instance "GroupController" has been initialized

But when I press the back button only ParticipantController gets deleted:

[GETX] CLOSE TO ROUTE /registration
[GETX] "ParticipantController" onDelete() called
[GETX] "ParticipantController" deleted from memory

And when I press once again to registration screen. The ParticipantController gets initialized and deleted plus the other controllers are being deleted.

[GETX] GOING TO ROUTE /registration
[GETX] Instance "ParticipantController" has been created
[GETX] Instance "ParticipantController" has been initialized
[GETX] "RegistrationController" onDelete() called
[GETX] "RegistrationController" deleted from memory
[GETX] "ParticipantController" onDelete() called
[GETX] "ParticipantController" deleted from memory
[GETX] "GroupController" onDelete() called
[GETX] "GroupController" deleted from memory

  • This seven minute video presents a good detailed description of “why not getx”: youtu.be/zlIgy4es5Ts

    – 

I found the answer. Get.lazyPut() requires fenix: true in order to reinitialize the controllers. I changed my bindings to the following and it works flawless (so far).

class RegistrationBindings implements Bindings {
  @override
  void dependencies() {
    Get.lazyPut<ParticipantController>(() => ParticipantController(), fenix: true);
    Get.lazyPut<GroupController>(() => GroupController(), fenix: true);
    Get.lazyPut<RegistrationController>(() => RegistrationController(), fenix: true);
  }
}

Leave a Comment