Pagination in Flutter using Pageview.builder

2,588

I don't know if this problem still exists (it's been six months since you've asked), but since this question still doesn't have an answer that is marked as correct I'll try.

If I understand correctly you want to load more items into your PageView once you've reached the last item of your PageView. You don't need a listener in your initState for that. You can just check if you've reached the last item in onPageChanged and then load more items.

It should work like this:

PageView.builder(
                      controller: _pageController,
                      itemCount: _items.length,
                      onPageChanged: (i) {
                        
                        if (i == _items.length - 1) {
                         getMoreItems().then((value) {
                            setState(() {
                              _items= value;
                            });
                          });
                        }
                      },
)
Share:
2,588
Farhan Nawaz
Author by

Farhan Nawaz

Updated on December 23, 2022

Comments

  • Farhan Nawaz
    Farhan Nawaz over 1 year

    I'm trying to implement pagination but I can't find any examples of how I should create the controller Listener function - or where I should put it. Please advise. Let me know if I should add more info too.

    Currently, my listener function looks like this:

    (within initState)
        pagecontroller.addListener(() {
          print(pagecontroller.page);
          if (pagecontroller.page == _postslist.length-1) {
            fetchMore();
          }
        });
    

    What happens currently is that the function is only called once, and subsequently never called later on.