有没有办法为flutter列表视图中的每个项目提供自定义ID?
我需要通过其 id 或键更新 flutter listview 中的单个项目。
为什么我想通过Id更新?
因为我想更新特定项目而不重建整个列表视图项目,这可能会提高我的应用程序的性能。
我想你可以只使用itemBuilder's 索引,因为它不是重复的。
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, int index) {
return ListTile(key: new Key(index.toString()));...
},
);
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以这样做:
return ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(list[index]),
onTap: () { // <-- onTap
setState(() {
list.insert(index, 'updated value');
});
},
onLongPress: () { // <-- onLongPress
setState(() {
list.removeAt(index);
});
},
),
);
},
);
Run Code Online (Sandbox Code Playgroud)
在这里,您可以看到您可以轻松更新和删除。
| 归档时间: |
|
| 查看次数: |
5265 次 |
| 最近记录: |