尝试使用 Obx 构建时 GetX 抛出 TypeError

GetX Throwing TypeError when try to build with Obx

我有控制器调用 MenuController

class MenuController extends GetxController {
  var currentCategory = Rx<int>(0);
  @override
  void onReady() async {
    await Future.delayed(const Duration(milliseconds: 2000));
  }

  void setMenuByIndex(int index) {
    currentCategory.value = index;
  }
}

我正在尝试从这样的简单小部件中检查选定的索引

Obx(() => Text(controller.currentCategory.value.toString()))

像这里一样获取控制器

final controller = Get.find<MenuController>();

我遇到了错误

type 'int' is not a subtype of type 'Rx<int>' of 'function result'

试试这个...

 class MenuController extends GetxController {
  Rx<int> currentCategory = Rx<int>(0);
  @override
  void onReady() async {
    await Future.delayed(const Duration(milliseconds: 2000));
  }

  void setMenuByIndex(int index) {
    currentCategory.value = index;
  }
}