Grinn Shot
Grinn Shot

Reputation: 13

Why is the scroll listener in flutter calling twice when i scroll to the end

I get duplicate results every time the data at the end of a scroll delays to fetch. This is a problem since I assume users may scroll down more than once whenever there is a delay and that causes duplicate results

 Future<void> _scrollListener() async {
if (scroll_controller.offset >=
        scroll_controller.position.maxScrollExtent &&
    !scroll_controller.position.outOfRange) {
 
  getdat();
}

}

Upvotes: 1

Views: 1217

Answers (1)

mkobuolys
mkobuolys

Reputation: 5343

That's true - this condition will trigger the function every time you get to the bottom of the list. To cope with this, you should handle the state explicitly. For instance, have a local variable (boolean flag) that you would switch to true/false based on whether the data is loading or not. This way, you could ensure that you are not triggering the data load multiple times.

For instance, depending on your state management solution, you could implement some kind of throttling solution. Here is the infinite list example that uses BLoCs for state management. And here you could notice how throttling is implemented to cope with overlaying requests in a short period.

Upvotes: 3

Related Questions