我想重新排序我的列表,但出现此错误。 此小部件的所有子项都必须在 Reorderable Listview 中有一个键
Widget list(list) {
return Scrollbar(
child: ReorderableListView(
header: null,
onReorder: _onReorder,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: <Widget>[
ListView.builder(
itemCount: list.length,
shrinkWrap: true,
itemBuilder: (contex, index) {
return _singleToDoWidget(list[index], index);
},
)
]),
);
}
最佳答案
键
用于唯一标识列表中的每个项目,因为您将移动它们。
你可以这样做:
ReoderableListView(
header: null,
onReorder: _onReorder,
children: [
for(var i; i < list.length; i++) {
Container(
key: ValueKey(list[index]),
child: _singleToDoWidget(list[index], index),
}
],
);
或者您可以在小部件函数本身中指定键
。
Widget _singleToDoWidget(String title, int index) {
return YourRootWidget(
key: ValueKey(title),
...
),
}
只是试图说明您的实现以及 key
将去向
关于flutter - 此小部件的所有子项都必须在 Reorderable Listview 中有一个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57805166/