Reputation: 165
A page contains a ListView
whose itemBuilder
returns a StatefulWidget
, but after calling setState in this page, new data retrieved but ListView will not update until scrolling current existing items out of screen.
Replacing items with StatelessWidget
works but my items need to be stateful because each of them has some animations.
How do I refresh the ListView
?
ListView.builder(
padding: EdgeInsets.only(top: listMarginTop, bottom: marginBottom),
controller: scrollController,
itemCount: cellCount,
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.fromLTRB(marginLeft, verticalSpacing, marginRight, 0),
height: itemHeight,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: itemWidth,
height: itemHeight,
child: getItem(index * 2, Size(itemWidth, itemHeight)),///return a StatefulWidget
),
Container(
width: itemWidth,
height: itemHeight,
child: getItem(index * 2 + 1, Size(itemWidth, itemHeight)),
)
],
),
);
},
),
Upvotes: 3
Views: 4137
Reputation: 121
I had the same problem.
I have added to my Listview : key: UniqueKey()
And it works well, no need anything else :)
Upvotes: 11
Reputation: 7869
You should call setState()
function inside your getItem()
function when your data changes in order to apply them to your Listview
items, this is supported by the fact that your data changes when you scroll items out of the screen:
getItem(){
//your implementation
setState();
}
Upvotes: 0