Provider: Error: Could not find the correct Provider above this Widget

Created on 19 Mar 2020  路  9Comments  路  Source: rrousselGit/provider

I am getting this error:

Error: Could not find the correct Provider<Trip> above this TravelTicketsScreen Widget

To fix, please:

  • Ensure the Provider<Trip> is an ancestor to this TravelTicketsScreen Widget
  • Provide types to Provider<Trip>
  • Provide types to Consumer<Trip>
  • Provide types to Provider.of<Trip>()
  • Ensure the correct context is being used.

If none of these solutions work, please file a bug at:
https://github.com/rrousselGit/provider/issues

Here are my Routes:

MainScreen>TripScreen > TravelTicketsScreen.

On the MainScreen the user selects a Trip and I push the new route, surrounded by a Provider<Trip>, so all future Routes will have access to this same Trip object:

() async {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) {
        return Provider<Trip>.value(
          value: trip,
          child: TripScreen(),
        );
      },
      settings: RouteSettings(name: '/trip'),
    ),
  );
},

Then on the TripScreen I can get the Trip using Provider.of<Trip>(context), there are no errors, it works fine. On TripScreen I show several buttons, each button leads to a new Route, I programmatically get what's the new Route with createCategoryRoute when a user clicks a button with this code:

() {
  Widget newRoute = createCategoryRoute(key, trip);
   if (newRoute != null) {
     Navigator.push(
       context,
       MaterialPageRoute(
         builder: (context) => newRoute,
         settings: RouteSettings(
           name: '/${newRoute.runtimeType}',
         ),
       ),
     );
   }
 }

This pushes me to the TravelTicketsScreen. On the TravelTicketsScreen I try to get the Trip as the first thing I do on build, like this:

@override
  Widget build(BuildContext context) {
    Trip trip = Provider.of<Trip>(context);

But then I get the error I showed on the begging of this Issue.

I am sure Provider<Trip> is an ancestor, as we can see it in to following image as an ancestor to TripScreen:

image

CategoriesContainer is where I push TravelTicketsScreen.

All 9 comments

Ok, I found out that when I push TravelTicketsScreen it's getting "parallel" to TripScreen, so Provider<Trip> isn't an ancestor, but how's this even possible?!

Before:

image

After:

image

That's how navigator works.
Each route are in a different branch.

You never have a situation where a route is "inside" another route.

So how can I make the Trip object be provided to all routes navigated after TripScreen?

So far I was using Provider as a wrapper to my MaterialApp:

image

Putting it above MaterialApp, or reinserting the provider on the new route everytime

Is this the same for other InheritedWidgets like Theme or just Provider?

It is the same for all InheritedWidgets

That could be fixed through declarative overlays though https://github.com/flutter/flutter/issues/50961

As the overlay would be part of the main branch

Oh that's great than.

Do I leave this open or should I close it then?

Let close it

It's not actionable as part of provider. It's either a modification on Flutter or a new package

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanekyy picture sanekyy  路  5Comments

mhd-barikhan picture mhd-barikhan  路  5Comments

MaxTenco picture MaxTenco  路  5Comments

Vadaski picture Vadaski  路  3Comments

prasadsunny1 picture prasadsunny1  路  6Comments