How to watch a specific property of a state like mobx @observabe #506
Unanswered
TrustOkoroego
asked this question in
Q&A
Replies: 2 comments 1 reply
-
🖐 You just need to create one provider per value watched, as mentioned in the doc final emailProvider = Provider<String>((ref) {
final store = ref.watch(storeProvider);
return store.email;
});
final phoneProvider = Provider<String>((ref) {
final store = ref.watch(storeProvider);
return store.phone;
}); Then your widgets are: class EmailWidget extends ConsumerWidget {
const EmailWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, ScopedReader watch) {
final email = watch(emailProvider);
return Text(email);
}
}
class PhoneWidget extends ConsumerWidget {
const PhoneWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, ScopedReader watch) {
final phone = watch(phoneProvider);
return Text(phone);
}
} Don't be afraid to create a lot of providers to watch each value of your store, that's the way riverpod works. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks julienlebren for your feedback!
This is what I am trying to avoid. With the scale of my project, this will quickly get messy. I will stick to Mobx and probably use riverpod for DI. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am migrating from Mobx to riverpod, but I seem not to find a way to implement the logic of mobx observable properties in riverpod. So basically in mobx I could have this:
`
@observable
String email;
@observable
String phone;
`
Then I could observe any of the properties independently.
Observer( builder: (_) => Text( '${registration.email}' ) )
So if email value changes in the store,only the widget observing email property will be rebuilt, not the widgets listening to the phone property. In riverpod you watch the entire state, and this will cause all the widgets to be rebuilt even when the actual value in the state being used by the widget did not change.
Is there a way to prevent the entire widget from rebuilding?
Beta Was this translation helpful? Give feedback.
All reactions