
我试图用Reorderable包创建一个可重排序项的列表;我有一个可重新排序的包装,其中有可重排序的列。我用了两个不同的列表。
List<Widget> items = [];
List childrenItems = [];列表是根据如下所示的数据创建的
List taskList = [
// First child Item
{
"name":"NOrbert kRoSs",
"sub-children":[
"child1","child2"
],
},
// Second child Item
{
"name":"Genius",
"sub-children":[
"Relerx","Yiuja","No filter"
],
},
];用于填充列表的方法。
generateList(List incoming){
List<Widget> children = [];
children.clear();
if(childrenItems.length > 0) {childrenItems.clear();}
if(items != null) items.clear();
if( incoming != null )
for(int n=0; n<=incoming.length-1;n++){
children.clear();
for(int sub = 0; sub<=incoming[n]["sub-children"].length-1;sub++){
children.add(
cardWidget(
header: incoming[n]["sub-children"][sub]["name"].toString(),
key: sub.toString(),
parentKey: incoming != null?n.toString():"0",
),
);
}
childrenItems.insert(n, children);
items.add(
mainListItem(
nPos: n,
header:incoming !=null?incoming[n]["name"].toString():"Aberor",
mainKeys: n.toString(),
),
);
}
}可重排序的
他们
ReorderableWrap(
children: items != null?
items:
(<Widget>[
Container(
key: ValueKey("newVal"),),
]),
onReorder: setRedorder),
ReorderableColumn(
children: childrenItems[nPos] != null?
childrenItems[nPos]:
(<Widget>[
Container(
height: 30,
color: Colors.redAccent,
width: 60.0,
key: ValueKey("value"),
),
]),
onReorder: setNewOrder,
), 问题当我运行应用程序时,我得到下面的错误
Duplicate GlobalKeys detected in widget tree.
The following GlobalKeys were specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The keys were:
[GlobalObjectKey ValueKey<String>#c9f0c]
[GlobalObjectKey ValueKey<String>#7990e]
[GlobalObjectKey ValueKey<String>#89542]
This was determined by noticing that after widgets with the above global keys were moved out of their respective previous parents, those previous parents never updated during this frame, meaning that they either did not update at all or updated before the widgets were moved, in either case implying that they still think that they should have a child with those global keys.
The specific parents that did not update after having one or more children forcibly removed due to GlobalKey reparenting are:
Column(direction: vertical, mainAxisAlignment: start, mainAxisSize: min, crossAxisAlignment: center, renderObject: RenderFlex#1c68d relayoutBoundary=up36),我是一个100%的肯定没有重复的键,并从错误信息,它的父母没有更新。如何修复这个?
发布于 2021-04-26 21:39:42
因为某种原因我不明白
children = [];而不是
children.clear();解决了我的问题。
发布于 2021-04-23 02:31:04
尝试使用不同的ValueKey。
key.dart中的描述指出,在具有相同父级的Element中,LocalKey(ValueKey extended LocalKey)必须是唯一的。
https://stackoverflow.com/questions/67222245
复制相似问题