Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
francescreig opened this issue Aug 25, 2023 · 2 comments
Open

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

francescreig opened this issue Aug 25, 2023 · 2 comments

Comments

@francescreig
Copy link

francescreig commented Aug 25, 2023

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
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants