In my game, I've got a soundProvider = Provider<MySoundClass>
so various Widgets can command changes to sound. It stores long-lived audio resources. I also have a gameStateProvider = ChangeNotifierProvider<GameState>
holding the game's state.
I'd like soundProvider
to watch gameStateProvider
, so that e.g. when the game is won, soundProvider
can change from playing normal music to playing victory music.
But doing so would trigger a recreation of soundProvider
's underlying MySoundClass
when gameStateProvider
notified it, which would leak the long-lived audio resources left lying around in the discarded MySoundClass
.
How should I structure this to prevent the leak but play victory music? Should I use ref.listen(gameStateProvider)
callbacks within soundProvider
, instead of ref.watch
, to play victory music? Or should I store the long-held audio resources in a third provider which soundProvider
watches? The former seems a little difficult to manage and the latter seems a little too boilerplate.