ListView itemBuilder method triggered on scroll
ListView.separated
re-triggered its itemBuilder
while scrolling, as it only loads items that are shown on screen, unlike ListView which loads all the items at the time of initialization, due to this ListView
is not a good option for a long list.
This increases performance significantly when we have a long list and complex widget to render as an item of ListView.separated
. This is the expected behavior mentioned in the question.
Reedy
Updated on December 29, 2022Comments
-
Reedy over 1 year
I am using a ListView.separated with the following itemBuilder:
itemBuilder: (context, index) { return _buildRow(dataList[index]); }
I noticed that this _buildRow() methods is triggered while I am scrolling. It seems to be triggered only the rows that are out of the screen while scrolling.
I do not use the setState() method within _buildRow(). Also, I verified that the super method build(BuildContext context) is not triggered. It really only is the itemBuilder one.
I am not sure if this is a normal behavior. Is there a way to avoid the itemBuilder method to be re-triggered on scroll? If this is not possible, is there a way to trigger a method once the ListView build has been completed?
-
Wiktor almost 3 yearsI believe that this answer should clarify everything: stackoverflow.com/a/56272858/12964903
-
towhid almost 3 yearsYes, it's completely normal behavior. ListView builds its child according to needs. If your child is outside the list area, it doesn't build that child. Also when a child is shown on the screen and going out of the list area, it recycles that view for the next upcoming child pretty effeciently.
-