How can we paginate when before we reach max extent in flutter ListView?

Viewed 355

I am currently paginating at max extent. I have written this code :

if (scrollController.position.pixels ==
        scrollController.position.maxScrollExtent) {
      setState(() {
        startIndex += 10;
      });
      context.read<MyBloc>().add(
            GetDataFromMyBloc(startIndex),
          );
    }

I have tried this test also :

 if (scrollController.position.pixels >
        scrollController.position.maxScrollExtent - 200) {}

But it's making scroll very laggy, because API is called multiple times. How can I make it call only one time ?

2 Answers

== is not a good practice in this case. try using something like:

if (scrollController.position.pixels >
        scrollController.position.maxScrollExtent - 200) {}

just you should handle in your stateful widget or your bloc not to fetch data multiple times.

_scrollController.addListener(() {
  if (_scrollController.position.isScrollingNotifier.value ||
      _scrollController.position.extentAfter < 500) {
    if (_scrollController.position.atEdge) {
      if (_scrollController.position.pixels == 0) {
        // You're at the top.
      } else {
        // you are at the above bottom
        }
      }
    }
  }
});

This may be helpful for you

Related