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

Duplicate page loads #35

Open
jonjomckay opened this issue Jun 16, 2023 · 4 comments
Open

Duplicate page loads #35

jonjomckay opened this issue Jun 16, 2023 · 4 comments

Comments

@jonjomckay
Copy link

Hello. I occasionally seem to get the first page of results loading twice, and I've narrowed it down to this line of code.

It appears that the method is called twice (as expected, I think), but previousPageKeys isn't yet populated on the second call. The first page key only seems to get added to previousPageKeys after _load is called the second time.

@pmkod
Copy link

pmkod commented Jun 19, 2023

Hello. I occasionally seem to get the first page of results loading twice, and I've narrowed it down to this line of code.

It appears that the method is called twice (as expected, I think), but previousPageKeys isn't yet populated on the second call. The first page key only seems to get added to previousPageKeys after _load is called the second time.

For me too, the page is reloaded twice, when I leave it and return to it.

@pmkod
Copy link

pmkod commented Jun 22, 2023

I'm sorry, it was due to poor implementation on my part. In your load function, you check the list of previousKeys using the .contains function. It turns out that I was using a specific type for my keys and I didn't overload the == operator to get a good equality comparison. I used the equatable library for this, and everything works correctly now. Thanks for your library.

@Arjun544
Copy link

@pmkod Can you please share some code?

@dJani97
Copy link

dJani97 commented Dec 1, 2024

Same issue here. For some reason, load is called twice so quickly that first call doesn't have time to finish.

For now I solved this using mutex. If another load call is still in progress, then I just return null from the load function:

Here is my implementation:

mutex provider, keyed by the page:

typedef MutexFamily = (int page, int limit);
final mutexProvider = Provider.family<Mutex, MutexFamily>((ref, family) => Mutex());

load:

load: (page, limit) async {
  final mutex = ref.read(mutexProvider((page, limit)));

  if (mutex.isLocked) {
    print('PagedNotifier.load is throttled, returning empty page');
    return await mutex.protect(() async {
      // awaits previous call before returning, to avoid showing `noItemsFoundIndicatorBuilder` for a brief moment.
      // you don't have to put any instructions here, just awaiting .protect() does to job.
    }); 
    return null;
  }

  return mutex.protect(() async {
    final items = // copy your usual async loading logic here
    return items; 
  });
},

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

4 participants