jonataslaw / getx

Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
MIT License
10.43k stars 1.63k forks source link

Listen to a field of an obs object #2557

Open nedStarck opened 2 years ago

nedStarck commented 2 years ago

Listen to a field of an abs object.

I want to listen to changes of a field from obs object, for example,

class MyEntity{
 int id;
 String name;
 .
 .
 .
}

I declare my entity in getx controller as obs value;

// in the getx controller
var myEntity = MyEntity().obs;

Now I want to follow changes of id field by ever() worker or listen method. But I must declare id field in my entity as an obs value as follow:

class MyEntity{
 var id = 0.obs;
 String name;
 .
 .
 .
}

The entity classes mustn't depend on state management.

Resolve this problem, pls. @jonataslaw

kauemurakami commented 2 years ago

just create your Rx<Int>? types id; And use obj.update((val)=> val.id = 1);

SoObx(()=> Text(obj.id.value) will be heard

nedStarck commented 2 years ago

But the entity depends on Rx which use in state management. Is that true?

kauemurakami commented 2 years ago

Usually just Entity().obs does the trick. Any object change, like id = 1, should change the id value and you will only use this in your controller, it is not directly linked to the model. check your code, probably a basic logic error. I'll give you an example of something I use here.

I have a service AuthService with User().obs.

I use this observable object object to load my observable user. final user = User().obs; In my controller when I want to update this value I do:

onChangedPassword(v) => auth.user.update((val) => val!.password = v);

and work :)

lalitf commented 2 years ago

Usually just Entity().obs does the trick. Any object change, like id = 1, should change the id value and you will only use this in your controller, it is not directly linked to the model. check your code, probably a basic logic error. I'll give you an example of something I use here.

I have a service AuthService with User().obs.

I use this observable object object to load my observable user. final user = User().obs; In my controller when I want to update this value I do:

onChangedPassword(v) => auth.user.update((val) => val!.password = v);

and work :)

This solved my problem Thanks

ghost commented 1 year ago

I have a similar problem using multiple fields from an Object. Obx Widgets are rebuilding even if I update just the name of the user, it will also rebuild the age widget

class User{ String name; int age; }

Inside the controller I got: final user = User().obs; updateUserName(name) => user.update((val) => val!.setName(name)); updateUserAge(age) => user.update((val) => val!.setAge(age));

And my Obx widgets are pretty simple and look like this:

Obx(() { print("name rebuild"); return Text('Name: ${controller.user.value.name}'); }), Obx(() { print("age rebuild"); return Text('Age: ${controller.user.value.age}'); }),

I have not seen any examples on how to listen to just one field. Both widgets are being rebuild, any idea how can I listen to just one field? The user has a lot more fields

ghost commented 1 year ago

I have changed the fields inside User class to be RxString and RxInt and I am now using ObxValue(controller.user.value.name) instead of Obx().

Is this good practice?