Is there an alternative method to initialize Provider rather than wrapping it with MaterialApp with MultiProvider?
I am currently using Provider to manage state in my Flutter app. I am wrapping my MaterialApp widget with MultiProvider to initialize all of my providers. However, I am wondering if there is a more efficient way to do this.
I have tried to create a provider while building a widget, but the issue right here was when navigating to another screen it shows provider not found exception and if we create a new instance on that widget the entire data is change.
Is there a way to initialize Provider without wrapping it with MaterialApp with MultiProvider? If so, what is the best way to do this?
Thanks for your help!
2
Answers
I’m afraid there’s no alternative, if you want to declare multiple providers you will need to implement
MultiProvider
at some point, about using MaterialApp, that’s up to you, since you can implementMultiProvider
without usingMaterialApp
but I don’t recommend you to do that since MaterialApp defines some constraints to how your app is rendered like margins to the top. About the other part of your question when you define a provider inside a widget this will only be available for child widgets this means widgets inside the parent one like:Now that this is clear I would like to know, why do you want to avoid the use of
MultiProvider
is there an specific reason for that?You can create the providing on current route, but to access it, you need to separate the context.
Now to pass the current provider on next route
Test snippet