Search code examples
flutterdartflutter-getx

GetX Throwing TypeError when try to build with Obx


I have controller call 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;
  }
}

And I'm trying to check selected index from simple Widget like this

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

Getting controller like here

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

Im getting the error

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

Solution

  • try this...

     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;
      }
    }