找不到 GetX 控制器

GetX controller not found

我有一个用户文件夹,在文件夹内:

  1. 绑定
  2. 控制器
  3. 观看次数

在我的绑定文件中,我写了

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

在我的视图文件中,我尝试调用 controller.count 并且有一个错误说找不到 UserController。 如果我将这段代码添加到我的视图文件中,那么它就可以工作了。我不明白为什么我的绑定不起作用。

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

编辑: 我找到了这个问题的答案。 我没有在我的视图文件中使用 GetBuilder。这就是为什么它一直说找不到我的控制器。

我认为您必须在视图和绑定之间建立联系。您可以在 GetMaterialApp 中这样做:

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

或者当您导航到页面时:

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

更多信息:https://chornthorn.github.io/getx-docs/dependency-management/binding/