Skip to content

Access current relevant context from within provider #50231

Closed

Description

@royibernthal

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:

  1. 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.

  2. 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.

  3. 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

  1. added
    d: stackoverflowGood question for Stack Overflow
    p: material_uimaterial_ui package in flutter/packages
    f: routesNavigator, Router, and related APIs.
    frameworkflutter/packages/flutter repository. See also f: labels.
    would be a good packageSeparate Flutter package should be made for this
    and removed on Feb 7, 2020
  2. iapicca commented on Feb 7, 2020

    @iapicca

    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

  3. royibernthal commented on Feb 7, 2020

    @royibernthal
    Author

    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.

  4. iapicca commented on Feb 8, 2020

    @iapicca

    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

  5. royibernthal commented on Feb 8, 2020

    @royibernthal
    Author

    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 :)

  6. added
    c: proposalA detailed proposal for a change to Flutter
    c: new featureNothing broken; request for a new capability
    and removed
    d: stackoverflowGood question for Stack Overflow
    on Feb 10, 2020
  7. chunhtai commented on Feb 10, 2020

    @chunhtai
    Contributor

    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.

  8. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Nov 19, 2020
  9. TahaTesser commented on Nov 19, 2020

    @TahaTesser
    Contributor

    @royibernthal
    Please provide the details asked above

  10. pedromassangocode commented on Dec 11, 2020

    @pedromassangocode

    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.

  11. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 11, 2020
  12. github-actions commented on Aug 8, 2021

    @github-actions

    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 -v and a minimal reproduction of the issue.

  13. locked as resolved and limited conversation to collaborators on Aug 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterf: routesNavigator, Router, and related APIs.frameworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packageswould be a good packageSeparate Flutter package should be made for this

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions