I'm using Obs/Obx in 2 routes, one works fine but one doesn't. There are 2 obs vars in the controller.
import 'package:get/get.dart';
class CountController extends GetxController {
final vcount = 0.obs;
final ncount = 0.obs;
static CountController get to => Get.find<CountController>();
@override
void onInit() {
super.onInit();
}
setVcount(int vehnum) {
vcount(vehnum);
}
setNcount(int notenum) {
ncount(notenum);
}
}
The one I use on a ListTile in the app drawer (vcount) works as expected. The other one is on an appbar action, the only action on this appbar. I want to disable the TextButton if ncount is 0.
appBar: AppBar(
actions: <Widget> [
Obx(() =>
TextButton(
child: _appBarChild(),
onPressed: () => nCountController.ncount.value == 0 ? null : _switchView(),
// onPressed: () => _switchView(),
),
),
],
)
The ncount value is set from a child of the route.
nCountController.setNcount(_queryResult.length);
Everything is the same, import controller.dart, "final (nC)countController = CountController.to;", setting the obs vars etc, the only difference is the type of widget, but when I run it I get the error:
======== Exception caught by widgets library ======================================================= The following message was thrown building Obx(has builder, dirty, state: _ObxState#11cd1): [Get] the improper use of a GetX has been detected. You should only use GetX or Obx for the specific widget that will be updated. If you are seeing this error, you probably did not insert any observable variables into GetX/Obx or insert them outside the scope that GetX considers suitable for an update (example: GetX => HeavyWidget => variableObservable). If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.
Am I missing something or doesn't Obx work in an appbar?