0

I'm building a basic Flutter app with Bloc pattern. This is my structure so far.

enter image description here

MainProvider contains the Repository, which I can pass in every Bloc of every sub page (one Bloc per page, basically). The problem is that, if I want to access MainProvider like this:

final provider = MainProvider.of(context);

from MenuPage, or OtherPage, which I access to by navigating from HomePage with

Navigator.push(context, 
    MaterialRoutePage(
       (context) => MenuPage(homePageParam); //or OtherPage(homePageParam)
    );

the .of method returns null.

How can I properly access that InheritedWidget? Should I do another type of Navigator.push?

riciloma
  • 1,456
  • 2
  • 16
  • 31
  • Can you include the code of your `MainProvider.of()` function ? – Mazin Ibrahim May 16 '19 at 18:16
  • 1
    For `InheritedWidget.of` to work, the build context you're passing should be below the Inherited widget in the widget tree. Assuming from the diagram above. The `MainProvider` and `MenuPage` are on different routes which won't work. You will have to place the InheritedWidget above your `MaterialApp` widget if you want it to be passed to all routes. – Sakchham May 16 '19 at 18:35
  • 1
    @MazinIbrahim, it's just the usual `context.inheritWidgetFromExactType(MainProvider) as Main Provider;` @Sakchham I was afraid I had to do it...not much code change, but would that mean that I can call `Navigator.of(context).push` with a new `MaterialPageRoute` and still access the `InheritedWidget`? – riciloma May 16 '19 at 19:22

0 Answers0