not*_*eal 3 listview auto-update flutter stream-builder
我正在 Flutter 中使用ListView.builder。现在一切正常,我在回调方法中有以下代码行:
@override
void initState(){
items = new List();
for(int index = 0; index < dataList.length; index++){
items.add(...creating widget ..);
}
}
@override
Widget build(BuildContext context) {
return Container(
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: dataList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
width: width,
height: 160 ,
key: UniqueKey(),
padding: EdgeInsets.all(10),
child: items[index],
);
},
),
);
}
Run Code Online (Sandbox Code Playgroud)
问题是当 dataList 的数据更新时,如果数据值更新、删除或位置更改,我应该如何实现此列表以使列表更新 UI 。StreamBuilder 是解决方案吗?
如果它不断变化,那么使用StreamBuilder
例如: https: //bendyworks.com/blog/a-month-of-flutter-rendering-a-list-view-with-stream-builder
如果没有那么你可以使用FutureBuilder
例如: https: //medium.com/nonstopio/flutter-future-builder-with-list-view-builder-d7212314e8c9
或者,
如果 dataList 在应用程序内部发生更改(删除/更新),则调用setState()将更新ListView
| 归档时间: |
|
| 查看次数: |
10580 次 |
| 最近记录: |