0

I have two forms in a tab bar. I'm using Getx for state management. All fields in those forms listen to a .obs variable. The forms fields are updating to the variable changes. How ever one field only reflect changes when that page is revisited.

I'm using this variables

final _countryCode = ''.obs
String get countryCode =>  _countryCode.value;

final _phone = ''.obs
String get phone =>  _countryCode.value;

These variable are updated on init.

   void onInit(){
    super.onInit();
    _countryCode(user.countryCode); // assign country code
    _phone(user.number); // assign number
    }

 return Obx(() => IntlPhoneField(
   final UserController _controller = Get.find()
      enabled: true,
          showCountryFlag: false,
          iconPosition: IconPosition.trailing,
          autoValidate: false,
          initialValue: _controller.initialPhoneNumber,
          initialCountryCode: _controller.userCountryCode,
);

How ever this field is not updating on init. but updates after that page is revisited.. What's causing this? All other fields are updating without any issue. But this one will update only if its revisited. How can i solve this??

Febin Johnson
  • 277
  • 1
  • 6
  • 21
  • have you wrapped you widget using obs values with Observer.? if no, then refer to documentation properly. – Yash Kadiya Mar 29 '22 at 09:06
  • @YashKadiya you mean obx? I have wrapped the widget using Obx to listen to obs values – Febin Johnson Mar 29 '22 at 09:14
  • i see, have you implemented notifyChildrens() method in your controller.? – Yash Kadiya Mar 29 '22 at 10:51
  • For which package/widget you used for IntlPhoneField? – Zakaria Hossain Mar 29 '22 at 11:01
  • You should share the full `GetX` class because it's not clear what is happening with `initialPhoneNumber` – Loren.A Mar 29 '22 at 11:39
  • @ZakariaHossain i'm using intl_phone_field package – Febin Johnson Mar 29 '22 at 15:13
  • @Loren.A im assigning values to obs. variables on init. values for initialphonenumber and usercountry code – Febin Johnson Mar 29 '22 at 15:21
  • @FebinJohnson the reason its not updating depends on a few factors, none of which we can see with the code you shared. We also can't see when and how `Get.put` is being called, which is when `onInit` happens. If you want someone to be able to copy and paste your setup to test out whats going on, then I suggest sharing the full page that's not updating, and the `Getx` class in question. – Loren.A Mar 29 '22 at 16:17

1 Answers1

0

It's better to use GetBuilder instead of Obx.

GetBuilder(
init: UserController,
builder: (_controller){
return IntlPhoneField(
      enabled: true,
          showCountryFlag: false,
          iconPosition: IconPosition.trailing,
          autoValidate: false,
          initialValue: _controller.initialPhoneNumber,
          initialCountryCode: _controller.userCountryCode,
);}),

There is also an initstate attribute for GetBuilder, You can get more details from here.

Jagal R Nath
  • 316
  • 5
  • 14