Search code examples
flutterdartriverpod

How should multiple Riverpod providers interact?


I'm new to Flutter and Riverpod and trying to understand how/when/where to use providers.

I am making a fairly simple game that has a board. That board can be re-used. I have a board model, board controller, and board widget. I want to re-use this widget on multiple views.

I'm creating a puzzle view (widget) that also has a puzzle controller and puzzle model. The board widget is on the puzzle view. I need to load initial state when we move to the puzzle view and so I tried to have the puzzle controller, which has info about the state the board will need, init the board state (via the boardcontroller), but Riverpod does not allow the state to be updated from inside another provider.

Any suggested architectures I should be using here? I have dug into the web and I see different solutions (addPostFrameCallback seems like a hack), but I'm not sure how a re-usable component is supposed to interact with the containing view.


Solution

  • The solution I found was to use ref.listen inside the constructor for the board controller to listen to the state of the puzzle controller. Since I will have multiple classed using the board controller I will need an interface or other means of implementing the functions board controller is expecting from it’s parent view.