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
605 stars 201 forks source link

Can't delete or insert item in paginated list #295

Closed lukehutch closed 8 months ago

lukehutch commented 8 months ago

I need to be able to delete an item from a paginated list, in the middle of one of the pages that has already been fetched.

e.g. for deletion, I tried keeping a copy of all the paginated items fetched so far, in itemList, then I filtered this to remove the item that should be deleted, to produce itemListWithItemDeleted, then I called

pagingController.itemList = itemListWithItemDeleted;

However, this does not actually update the view. The deleted item is still showing.

What is the purpose of setting the itemList, if the view is not going to be updated to reflect the new list?

How can I modify the paginated list, after pages have been fetched?

(Ideally this would support both insertion and deletion, but right now I need deletion...)

lukehutch commented 8 months ago

OK, I moved this one step further along, by forcing a rebuild of the widget tree using markNeedsBuild. However, now the pagination widget will shorten the number of items displayed to the new length of the list, when one or more items are deleted, but it just removes widgets from the end of the list, it doesn't check whether the list has changed.

lukehutch commented 8 months ago

In case anyone else runs into this ussue, I think the solution is to use a unique ValueKey for each item. However, I wrote my own grid view from scratch, so I'll close this now.

Trung15010802 commented 6 months ago

Hi, @lukehutch For insert item into list:

      _pagingController.itemList?.insert(0, item);
      // ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
      _pagingController.notifyListeners();
enchance commented 3 months ago

Hi, @lukehutch For insert item into list:

      _pagingController.itemList?.insert(0, item);
      // ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
      _pagingController.notifyListeners();

Do you know how to animate the addition of an item instead of just suddenly appearing?

lukehutch commented 3 months ago

Do you know how to animate the addition of an item instead of just suddenly appearing?

Yes, you can wrap each item in an ExpandedSection, and initially hide it by setting expand to false, then once it is added to the list, set expand to true and call setState in the parent widget to refresh the UI.

Note that you need to assign each item a unique ValueKey, otherwise Flutter won't be able to detect when items are added to or removed from the middle of the list, and you'll get some weird behavior.