gpt4 book ai didi

Flutter ListView 在 setState 之后不更新,使用 StatefulWidget 作为项目

转载 作者:IT王子 更新时间:2023-10-29 06:58:12 41 4
gpt4 key购买 nike

一个页面包含一个ListView,它的itemBuilder返回一个StatefulWidget,但是在这个页面调用setState后,获取到新的数据,但是ListView不会更新直到将当前现有项目滚动出屏幕。

StatelessWidget 替换项目是可行的,但我的项目需要是有状态的,因为每个项目都有一些动画。

如何刷新 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)),
)
],
),
);
},
),

最佳答案

我遇到了同样的问题。

我已将 key: UniqueKey() 添加到我的 Listview 中

而且效果很好,不需要其他任何东西:)

关于Flutter ListView 在 setState 之后不更新,使用 StatefulWidget 作为项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897163/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com