Search code examples
reduxdartflutterflutter-redux

Fetching data on corresponding redux store-state update in a component(widget)


I am using flutter-redux for state management in my project using StoreConnector.

Lets say I have a list of songs in the main view and a music player widget in the bottom bar. Everytime a new song is clicked on, the music player widget should make an api call for number of people who listened to that song. So I need to fetch data in this widget not just once but everytime the corresponding store data in _ViewModel for this widget changes.

In react js, we can use ComponentDiDUpdate() lifecycle. I wanted to know if there is something similar in flutter or if there is a best practice for this situation.


Solution

  • You can use StoreConnector event listeners:

    StoreConnector<AppState, AppState>(
      onDidChange: _onDidChange,
      onWillChange: _onWillChange,
    );