Accessing previous state using ref.state after a rebuild is not allowed #2476
AhmedLSayed9
started this conversation in
General
Replies: 2 comments 7 replies
-
Sounds like a bug more than anything |
Beta Was this translation helpful? Give feedback.
1 reply
-
Allowing It would mean that the number of times a provider rebuilds have an impact. Having a provider depend on its previous value also makes the code a lot of complex to reason with, and therefore less maintainable. |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Note: This discussion meant to be a reference for understanding the reason of that restriction and offer alternative solutions.
Straight to an example:
Invalidating
fooProvider
will lead to an exceptionTried to read the state of an uninitialized provider
while state already has a value.Reason: Accessing previous state using
ref.state
when the provider rebuild before using a setter again is not possible, That's because _debugDidSetState is set to false again when a provider rebuild. Is there a reason for that restriction?Alternative solution:
This work fine but any rebuild of the provider will break the intended behavior (as state will back to 0). So, we should only use
ref.listen
without any otherref.watch
which imo can mistakenly not to be followed.Beta Was this translation helpful? Give feedback.
All reactions