I was hoping to use InheritedWidget at the root level of my Flutter application to ensure that an authenticated user\'s details are available to all child widgets. Essential
Is it therefore not possible to use a Scaffold or a MaterialApp as the child of an InheritedWidget?
It is very possible to do this. I was struggling with this earlier and posted some details and sample code here.
You might want to make your App-level InheritedWidget the parent of the MaterialApp rather than the Scaffold widget.
I think this has more to do with how you are setting up your MaterialWidget, but I can't quite tell from the code snippets you have provided.
If you can add some more context, I will see if I can provide more.
MyInherited.of(context)
will basically look into the parent of the current context to see if there's a MyInherited
instantiated.
The problem is : Your inherited widget is instantiated within the current context.
=> No MyInherited
as parent
=> crash
The trick is to use a different context.
There are many solutions there. You could instantiate MyInherited
in another widget, so that the context
of your build method will have a MyInherited
as parent.
Or you could potentially use a Builder
to introduce a fake widget that will pass you it's context.
Example of builder :
return new MyInheritedWidget(
child: new Builder(
builder: (context) => new Scaffold(),
),
);
Another problem, for the same reasons, is that if you insert an inheritedWidget inside a route, it will not be available outside of this route.
The solution is simple here !
Put your MyInheritedWidget
above MaterialApp
.
above material :
new MyInherited(
child: new MaterialApp(
// ...
),
)