How to handle ref invalidation during async gaps? #3177
btrautmann
started this conversation in
General
Replies: 1 comment
-
Use ref.onDispose to detect if the provider was disposed during an await. That generally should help |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello 👋
I'm running into an issue that I've seen mentioned only a couple times, where I receive the following error between async gaps in
Provider
s, specifically those that areawait
ing anotherProvider
'sFuture
orStream
.[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:riverpod/src/framework/element.dart': Failed assertion: line 673 pos 7: '!_didChangeDependency': Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt
I understand what the error is telling me, and you highlight that in your comment here, but this happens in cases where there are no
await
s in theProvider
"chain" other than thoseawait
ing otherProvider
s.One such case is below:
If, during these async gaps, the
ref
is being invalidated, how am I supposed to guard against that? I don't see an API similar tomounted
forStateNotifier
.As a follow-up, am I abusing
ref.watch(provider.future)
? Is this an anti-pattern, or is this a pretty expected use case? It seems that these async gaps would be pretty commonplace inProvider
code utilizingFuture
/StreamProvider
, but I'm unsure if I'm just doing something dumb.Thanks for any help!
Beta Was this translation helpful? Give feedback.
All reactions