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

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

Open
ibrahimEltayfe opened this issue Aug 28, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ibrahimEltayfe
Copy link

ibrahimEltayfe commented Aug 28, 2024

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
    });
  1. Add a delay before the request is triggered, simulating a scenario where the request doesn't execute immediately say 5 seconds.

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

  3. 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;
  1. 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
class CodeSample extends StatefulWidget {
  const CodeSample({super.key});

  @override
  State<CodeSample> createState() => _CodeSampleState();
}

class _CodeSampleState extends State<CodeSample> {
  final PagingController<int, String> 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<int, String>(
          pagingController: pagingController,
          builderDelegate: PagedChildBuilderDelegate(
              itemBuilder: (BuildContext context, String item, int index) {
                return GestureDetector(
                  onTap: (){
                    List<String> 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
regular-scenario.mov

Issue

issue01.MOV
@ibrahimEltayfe ibrahimEltayfe added the bug Something isn't working label Aug 28, 2024
@yosifsamir
Copy link

yosifsamir commented Sep 1, 2024

we need a solution

@HasanShaddadKangaroo
Copy link

Again, we need a fix for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
@yosifsamir @ibrahimEltayfe @HasanShaddadKangaroo and others