Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading