erdemsword
erdemsword

Reputation: 89

Flutter [Get] the improper use of a GetX has been detected

I got this error. I am encountering this error while checking if one after another. I'm not sure how the code I wrote works. I wanted to consult you about whether it works or not. Can anyone help with this problem and solve it?

return Obx(() => _languageController.isLang == ''
    ? LanguageView()
    : controller.getCurrentUser == null
        ? LoginView()
        : _countryController.isLanguage == false
            ? CountryView()
            : HomeView());

[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.

Upvotes: 2

Views: 5582

Answers (1)

Baker
Baker

Reputation: 28090

For the code above to work & not throw the error you saw, isLang must be an "observable".

For example, something like:

class LanguageController extends GetxController {
  RxBool isLang = false.obs();
}

Since the code in the question doesn't show isLang's value being used (i.e. isLang.value), I'm guessing it's not an observable.

When using either GetX or Obx widgets, you must use "observables" (RxBool, RxInt, Rx<Whatever>, etc.). A regular int, bool, etc. inside your controller is not enough to satisfy that requirement. GetX and Obx require an observable type of Rx something.

Upvotes: 2

Related Questions