Henry Cho
Henry Cho

Reputation: 770

Provider across routes but after MaterialApp

I have a class which needs access to MaterialApp context, but it also needs global access from all routes.

To create a global provider, I can wrap MaterialApp with Provider, but such provider has no access to the context. Therefore, I have to provide the class after MaterialApp.

I realize I could wrap every single route with the provider because it is stateless, but I would like to know if there is a better way of doing this.

Thanks in advance!

Upvotes: 1

Views: 866

Answers (2)

Rémi Rousselet
Rémi Rousselet

Reputation: 277497

You can use builder or onGenerateRoute of MaterialApp

MaterialApp(
  builder: (context, child) {
    return Provider.value(
       value: // TO-DO use context
      child: child, 
    );
  }
) 

Upvotes: 5

Rodrigo Bastos
Rodrigo Bastos

Reputation: 2448

You can wrap the material app in a Builder to gain acess do a brand new context.

Upvotes: 0

Related Questions