Call Provider Variable out of a Build context

How can I call Provider variable out of a Build context class??
if is necessary a context paramns

Hi @Ed_Angelis_Ribeiro there is no straight way. It is the only big problem of that package. But you have multiple options to solve it.

Pass the context as a param tô the function calling Provider.


CallingSomeFunction(BuildContext context){<ProviderType>();

The only problem with this approach is if you reuse the function and call it from inside and outside the Build Method. If that is the case you will get an error saying you should use<ProviderType>() when calling a Provider outside the build method.

To solve this you could instead pass the variable/object you want to the function.

CallingSomeFunction(<ProviderType>() );

CallingSomeFunction(ProviderType provider){


Other things you can do is use a Package like get_it. This will allow you to access any provider without context.

Yo can also in some cases create a GlobalKey of the page you are in and call globalKey.currentContext, to access the context of that page. But I don’t recommend you this approach.

There are other solution and packages out there you can try. Like getX, but this would replace Provider in your project.

Finally if you are not going to realise the app, or if it is going to take you 6 months or so. You could change Provider for Riverpod, it is from the same guy that created Provider and other famous packages. It is VERY easy to use and introduces lots of new things. Like not needing context and Families (which is a way of having two Providers of the same Type, and accessing them individually).

Hope this helps :wink:


You could possibly add a globalkey to your scaffold and then use the currentContext attribute to fetch the context from outside.

1 Like

Thanks for your help, It work very well.