I will omit any code here, as this question is rather conceptual. I'm currently developing a Flutter App using riverpod to manage the components of the app-wide state, such as session tokens, etc.
I thus have methods which require access to the current riverpod state. I initially thought that this can be achieved by passing a variable of type Ref to every callback requiring any data stored within any Provider. Yet it seems that this is not really possible, as there's the ProviderRef, WidgetRef, Ref, etc. Using any of these types as the type of the according function parameter will not lead to correct code, e.g.:
- Say you have coded a wrapper method you use to execute HTTP API Requests in your app. Let's call that method
send(Ref providerReference), for simplicity. - When you want to use this method e.g. to initialize a provider, this works.
- When you try to use this method within a
ConsumerWidget, it will not work; as yoursend()function expects aRef, but you actually have aWidgetRefwithin yourConsumerWidget. - The same problem goes for the other way around.
So before simply transforming my send(Ref providerReference) into sendWithoutWidgetRef(Ref providerReference) and sendWithWidgetRef(WidgetRef providerReference), I just wondered if there's anything I'm misunderstanding regarding the use in that sense of riverpod?
This question has some kind of a similar context, but provides no real solution.
I know you could of course pass all the different kinds of providers to your send() method, e.g. do sth like send(AppLanguageProvider langRef,SessionTokenProvider sessionRef, ....), but requiring to fetch all these providers before calling send(), every single time you call send(), feels odd to me and kind of defeats the purpose of even using a provider. In that case it may be simple to store the current app language + session token in something like Flutter Secure Storage, and not even use riverpod at all.
So I really think that I'm misunderstanding something?