Repository navigation
Access current relevant context from within provider #50231
Description
I tried to publish this issue on stackoverflow as well, no luck there, thought I might as well try here.
https://stackoverflow.com/questions/60031949/flutter-access-current-relevant-context-from-within-provider
I have a provider - a class extending ChangeNotifier and injected into the app via a ChangeNotifierProvider.
The app has many routes, and the provider is available on all of them since it's injected at the root of the app.
That provider actually handles pub/sub in the background, and I'd like to be able to display toasts for certain events.
The problem is, in order to display a toast, I need to pass a relevant context, which I don't have.
How would you retrieve the current relevant context in my case?
At the moment I can't think of any solution that doesn't involve repetitive injection/listening in each route. Is there a built-in way to do this neatly?
I tried a few ideas:
-
I stored the context I got from a custom generateRoute method in a static class (since a provider can't notify changes at the route generation stage). It technically worked, except a route is not generated on route pop, and even the already generated previous route doesn't need to rebuild. In other words I don't have access to all route change events.
-
I created a RouteObserver. It indeed has lifecycle hooks which trigger at all possible route change events, however there's no way to access the context from a PageRoute. It's only possible to access things like RouteSettings.
-
I tried to pass the context I'm getting from ChangeNotifierProvider to the constructor, however Overlay.of(context) is null for that context.
If I understand correctly, this context is only passed once at the construction time of the provider, so I don't see how it could stay relevant after changing routes. (unless the provider is at a deeper level and is constantly being deconstructed and constructed).
I don't mind an out of the box solution for the specific issue of being able to create a toast from within a provider, no matter what route you're at - as long as it doesn't require manual and repetitive code which needs to be applied to every single route.
Activity
iapicca commentedon Feb 7, 2020
Hi @royibernthal
I understand your point,
not sure if it's best to have it "out-of-the-box" or as a package,
for sure if it was a package I'd use it.
Do you have some code sample to share?
It might be a nice add to this.
Thank you
royibernthal commentedon Feb 7, 2020
Hi @iapicca
Thanks for the response.
Frankly I've already deleted the failed experiments. Are any of my points unclear in your opinion?
I agree, such a package would have been great. I'll try to suggest it in the Navigator 2.0 issue.
iapicca commentedon Feb 8, 2020
Hi @royibernthal
this isn't polished code
and is more a cheap alternative rather than a solution
(usually when I don't know how to do something
I slap it in a singleton :) )
please take a look to this sample
I guess this could allow to access use the current context from anywhere.
Again, it's not necessarily a solution to your issue,
but I hope it can help.
Have a good weekend
royibernthal commentedon Feb 8, 2020
Hi @iapicca
It's indeed the most straightforward solution that comes to mind, and admittedly it's very similar to what I'm currently doing as a temporary solution.
The problem is it's very manual and repetitive, as well as prone to human error when adding more routes.
Have a great weekend :)
chunhtai commentedon Feb 10, 2020
hello @royibernthal
Do you mind share a code example to describe the issue? It is a bit hard to provide any suggestion without knowing the detail setup and the use case.
TahaTesser commentedon Nov 19, 2020
@royibernthal
Please provide the details asked above
pedromassangocode commentedon Dec 11, 2020
Without additional information, we are unfortunately not sure how to resolve this issue.
We are therefore reluctantly going to close this bug for now.
Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away!
Thanks for your contribution.
Could everyone who still has this problem please file a new issue with the exact description of what happens, logs, and the output of
flutter doctor -v.All system setups can be slightly different, so it's always better to open new issues and reference related issues.
github-actions commentedon Aug 8, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.