-
-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mitigate ProviderNotFoundException with context redirection #862
Comments
Interesting idea! Would certainly be helpful. Usually what I do is I grab references to the necessary context-dependent state objects ( I think the biggest challenge with your above example is if the inherited context goes stale you'll no longer be able to inherit fro it so you either:
Do you have thoughts for addressing this downside? Perhaps the answer is "for this use-pattern it's not a problem, don't use it". |
Yes the context going stale issue is a big one. It's particularly problematic when using things like |
Note that Riverpod already offers such a feature. In a way that's neater at that. Rather than having to list all providers by hand, you can tell ProviderScope to import all providers from the previous route. The context going stale issue is still one. But it could be solved in Riverpod specifically, as it isn't bound to the BuildContext. I just haven't done it yet |
I agree with @caseycrogers. I would see the stale problem as expected error. It's to the developer to choose which context he wants to wire in. The stale problem could be handled the same way Flutter already handles for it's own inherited widgets: Future<T?> showDialog<T>({
...
}) {
assert(_debugIsActive(context)); // we can do it also
assert(debugCheckHasMaterialLocalizations(context));
final CapturedThemes themes = InheritedTheme.capture(
from: context,
to: Navigator.of(
context,
rootNavigator: useRootNavigator,
).context, // <- flutter provides another "redirecting context"
); As for the showDialog(
context: context,
builder: (_) => Provider.redirect(
context: Navigator.of(context).context, // the developer provides a context that won't be stale
child: const HelloDialog(),
),
); Of course we can always improve documentation and asserts to improve DX. But still, for most use cases: showDialog, showModelBottomSheet, etc. The current context would just be fine. Thanks for the considerations @caseycrogers, @rrousselGit. |
The stale problem is different for provider vs Flutter's theme. Provider values are often disposable. And once disposed, a notifier cannot be used anymore. |
The logical solution to this, in my opinion, would be to use a nested navigator to provide your values accross all the pages that you need to have those Providers in. I haven't however found any satisifable solution that just lets me create new nested navigators that act completely transparent (meaning, navigating in either direction just feels like a single navigator). |
The Navigator is a smart solution, as the InheritedWidget will be scoped to the it. Thats what I actually do, making it possible to easily scope providers and access them (even on modals). You would depend on go_router thought. Code: |
Is your feature request related to a problem? Please describe.
Describe the solution you'd like
Proposal
We simply provide the "redirecting context".
Or a new dedicated InheritedWidget. Which would not break projects that actually use the above case.
Internally, provider would first check for a "redirecting context", and use it instead of throwing.
Now, finally.
Describe alternatives you've considered
All above are even worse if we consider pushed pages that require many different providers. Redirecting the context is scalable, simple and efficient.
Additional context
This proposal addresses a significant pain point in provider. Simplifying provider access across contexts will empower developers to build complex applications more effectively, reducing the need for workarounds and fostering best practices.
The text was updated successfully, but these errors were encountered: