When creating a Flutter application, we use a Navigator to move back and forth between screens.
In my application, I have some class instances that I want to share with all child widgets in the hierarchy.
An example is a Data Base driver class (stateful) which holds all functionality of reading/writing to the actual database.
Currently - the 'main' function is initializing it, then the resulting object is passed through all constructors of the participating widgets.
How do I make it behave like the Navigator so it would be available in the global scope without importing it or passing it through a chain of constructors?
I would gladly accept any other approaches, and still, if you can also address the exact question - I would be grateful :)
I opened the code of Navigator, Stateless and Stateful widgets, I tried to google for the source of that Navigator instance but came up with nothing helpful.
CodePudding user response:
Have you tried using InheritedWidget?
From the docs:
Base class for widgets that efficiently propagate information down the tree.
Create DataBaseDriver
class as inherited Widget class, access it in its child class using context
as DataBaseDriver.of(context)