Laxmikanth Madhyastha
Laxmikanth Madhyastha

Reputation: 342

Updating provider class from outside widget

I'm new into flutter and im using the provider package for state management and i have a typical scenario where messages are coming as event and handling this event is written in a separate class, as soon as a message arrives I have to update the message list which is defined in the provider class to which the other widgets are listening Now how can I update the message list in the provider class from the class where the event occurs? (I cannot use provider.of(context) since it is a plain class and i don't have access to buildcontext )

I have tried creating a new class of the provider class and then updating the message list but for some reason, it is creating a different instance of the provider class

Upvotes: 2

Views: 5813

Answers (1)

Abion47
Abion47

Reputation: 24781

Provider is a dependency injection package that specifically uses the widget tree (i.e. the build context) to pass around instances of classes. If you don't have access to a build context, you can't get the instance.

Instead, you're going to want to use something like get_it or flutter_bloc for state management that doesn't require a build context. Alternatively, you could change the class emitting the events to be a singleton.

Upvotes: 7

Related Questions