EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
626 stars 213 forks source link

How to handle the duplicates? #254

Closed nikiforosper closed 1 year ago

nikiforosper commented 1 year ago

Using my api, data is added every second So, page 1 is loaded and it is ok. But when page 2 loads and new data is added, then page 2 contains some of the data from the page 1. For example:

  1. Page 1: [1, 2, 3, 4, 5]
  2. Load page 2
  3. Page 1 = [1, 2, 3, 4, 5] Page 2 = [4, 5, 6, 7, 8]

My fetch Data function:

Future<void> _fetchPage(int pageKey) async {
    try {
      pg++;
      List<SearchClassified> newClassifieds = await SearchService.searchClassifieds(args, pg);
      bool isLastPage = newClassifieds.length < _pageSize;
      if (isLastPage) {
        _pagingController.appendLastPage(newClassifieds);
      } else {
        final nextPageKey = pageKey + newClassifieds.length;
        _pagingController.appendPage(newClassifieds, nextPageKey);
      }
    } catch (error) {
      if (mounted) {
        _pagingController.error = error;
      }
    }
  }
clragon commented 1 year ago

you can directly access and set the itemList property of the controller. you can also remove items from the list you are going to add before adding it, by comparing it against the list of existing items.

nikiforosper commented 1 year ago

I resolved this with _pagingController.itemList = _pagingController.itemList?.toSet().toList();

clragon commented 1 year ago

please close this issue then.