previous and next states have same values of variables in Riverpod 1.0.0. #904
Replies: 6 comments
-
Do you have a complete example? |
Beta Was this translation helpful? Give feedback.
-
@rrousselGit, I created a simple project for this. Here's the link. https://github.com/abdullahriaz95/riverpod_example |
Beta Was this translation helpful? Give feedback.
-
Tested your project and this is indeed the case. Listening to only the
It is by the way best practice to listen only to values you want your UI to change for. Otherwise, you also rebuild your UI if values change on which the UI does not depend. |
Beta Was this translation helpful? Give feedback.
-
@abdullahriaz95 That happens because listening a changenotifier will trigger any time notifyListeners is called, there is no way to tell if everything inside a changenotifier is different. That's a difference vs StateNotifier, which is able to tell when the internal state actually changed. |
Beta Was this translation helpful? Give feedback.
-
@davidmartos96 that's true, but if |
Beta Was this translation helpful? Give feedback.
-
@davidmartos96 Is right. The change notifier is mutable so how could riverpod automatically have the state before it was mutated? They both refer to the same object / ChangeNotifier. This is why it is preferable to use StateNotifier with a state object that you can 'copyWith' some values changed. @daniellampl shows one way to work around this by using the select modifier. Since the primitive values are immutable in dart they won't refer to the same state. However if you are mutating reference objects in a ChangeNotifier and use select to get to that reference you would run into the same problem. Closing as this isn't an issue with Riverpod. |
Beta Was this translation helpful? Give feedback.
-
As described in the changelog, now in 1.0.0 we can use:
ref.listen(providerName , (previous, next){});
But the previous and next instances of the provider have the same variable states even though I am changing them in my provider when called a function. Here it is:
Whereas it should vary. The previous and next providers should have opposite updatingProfile values.
Beta Was this translation helpful? Give feedback.
All reactions