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
624 stars 211 forks source link

[Bug] PageRequestListener gets triggered everytime i modify in itemList while a request is loading #344

Open ibrahimEltayfe opened 3 weeks ago

ibrahimEltayfe commented 3 weeks ago

Steps to reproduce

  1. Setup PagingController and add PageRequestListener callback

    final MyPagingController<int, MyItem> pagingController =
      MyPagingController<int, MyItem>(firstPageKey: 1);
    
    pagingController.addPageRequestListener((pageKey) {
      print("NOTIFIED");
      //put your request here
    });
  2. Add a delay before the request is triggered, simulating a scenario where the request doesn't execute immediately say 5 seconds.

  3. After the initial page request is handled, scroll to the end of the list, triggering the next page request.

  4. Modify the Item List while the second request is processing

    final List<MyItem> newList = List.from(pagingController?.itemList ?? []);
    
    // do your modification logic on newList for example, modify the isLiked variable in your list
    newList[index] = newList[index].copyWith(isLiked: !newList[index].isLiked);
    
    pagingController.itemList = newList;
  5. While the next page request is still loading, make modifications to the itemList seems to cause the addPageRequestListener to be triggered again

Expected results

Modifying the itemList in the pagingController should not retrigger the addPageRequestListener if there is an active request or while the request is being processed.

Actual results

Modifying the itemList during an active request causes the addPageRequestListener to be triggered again unexpectedly.

Package Version

4.0.0

Platform

Android, iOS

Code sample

Code sample ```dart class CodeSample extends StatefulWidget { const CodeSample({super.key}); @override State createState() => _CodeSampleState(); } class _CodeSampleState extends State { final PagingController pagingController = PagingController(firstPageKey: 0); @override void initState() { pagingController.addPageRequestListener((pageKey) { print("NOTIFIED: $pageKey"); Future.delayed(const Duration(seconds: 5), () { pagingController.appendPage(List.generate(20, (index) => "DUMMY ${index + (20 * pageKey)}"), pageKey + 1); },); }); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( body: PagedListView( pagingController: pagingController, builderDelegate: PagedChildBuilderDelegate( itemBuilder: (BuildContext context, String item, int index) { return GestureDetector( onTap: (){ List newItems = List.from(pagingController.itemList ?? []); int itemIndex = newItems.indexWhere((element) => element == item,); if(itemIndex != -1){ newItems[itemIndex] = "MODIFIED $index"; } pagingController.itemList = newItems; }, child: Padding( padding: const EdgeInsets.all(8), child: Text(item, style: const TextStyle(fontSize: 18),), ), ); } ) ), ); } } ```

Video

Screenshots / Video demonstration Regular scenario https://github.com/user-attachments/assets/25b43d95-c6f1-4c5e-a392-a8f8b2cdb0b9 Issue https://github.com/user-attachments/assets/c8c09593-10a2-404e-8b26-a15d53720560
yosifsamir commented 2 weeks ago

we need a solution