Search code examples
flutterdartflutter-getx

GetX controller not found


I have a User folder, inside the folder:

  1. bindings
  2. controllers
  3. views

In my bindings file, I have wrote

Get.lazyPut<UserController>(
      () => UserController(),
    );

In my view file, I try to call controller.count And there is an error saying that UserController not found. If I add this code to my view file then it works. I don't understand why my binding not working.

final UserController controller = Get.put(UserController());

Edit: I found the answer to this question. I did not use GetBuilder in my view file. That's why it keeps saying my controller not found.


Solution

  • I think you have to make the connection between your view and your binding. You can do that either in the GetMaterialApp like this:

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

    or when you navigate to a page:

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

    More here: https://chornthorn.github.io/getx-docs/dependency-management/binding/