ftognetto / riverpod_infinite_scroll

A plugin for Flutter's infinite_scroll_pagination that works with Riverpod.
MIT License
27 stars 23 forks source link

How we can change the firstPageKey to reevaluate query? #41

Open francescreig opened 1 year ago

francescreig commented 1 year ago

Imagine that you are using the same widget to render a list of items, but you want to apply one filter to the list, so that it refetches again the items, but with the updated query. In my case the filters (and pagination) are treated in the firstPageKey.

The problem resides in that when I rebuild the widget passing another filters to the firstPageKey, since the StateNotifierProvider is already alive, it does not update its values, since they are declared once when building the initial state (on super constructor). How we could be the best way to solve this problem? Refreshing the provider?

amitjangra01 commented 6 months ago

hey @francescreig what about the filterData() method inside the StateNotifier. i mean you can invoke filterData() method when applying a filter and then updating the state of your notifier accordingly. something like this

void filterData() async { try {

  // filter your data here 
  //final response = filterDataMethod(); /*make an api call to receive data*/

  state = state.copyWith(
      records: [
        ...response,
        // ...(state.records ?? []),
      ],
      nextPageKey: response.length < limit
          ? null
          : response[response.length - 1].id,
      previousPageKeys: {
        ...state.previousPageKeys,
       page,
      }.toList());
} catch (e) {
  logger.e(e.toString());
}
state = state.copyWith(
  records: [],
  nextPageKey: [],
  previousPageKeys: [],
);

}

Although i am also not an expert but i think it might work for you.

ftognetto commented 2 months ago

Hi @francescreig maybe you could use a family provider where the family is represented by the filters? Or else you can manually invalidate the provider using ref.refresh / ref.invalidate