英文:
Flutter: ReorderableListViewChildGlobalKey used by Multiple widgets error
问题
以下是您要翻译的内容:
"Error I get is: The key [_ReorderableListViewChildGlobalKey ValueKey#46dc3] was used by multiple widgets
Each item in my list is already uniquely identified in my dismissable widget.
Widget mainCardWidget(BuildContext context) {
return ReorderableListView(
onReorder: onReorder,
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
children: _getListItems(),
);
}
void onReorder(int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final Rung element = _items.removeAt(oldIndex);
_items.insert(newIndex, element);
});
}
List<Widget> _getListItems() => _items
.asMap()
.map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
.values
.toList();
Widget _buildTenableListTile(Rung item, int index) {
return Dismissible(
key: Key(item.rungId),
onDismissed: (direction) {
setState(() {
_items.removeAt(index);
_removeditems.add(item);
});
},
background: Container(color: Colors.red),
child: Padding(
padding: const EdgeInsets.all 20.0),
child: Card(child: cardWithInfoPage(item, context, index)),
),
);
}
}"
英文:
Error I get is: The key [_ReorderableListViewChildGlobalKey ValueKey#46dc3] was used by multiple widgets
Each item in my list is already uniquely identified in my dismissable widget.
Widget mainCardWidget(BuildContext context) {
return ReorderableListView(
onReorder: onReorder,
physics: const BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
children: _getListItems(),
);
}
void onReorder(int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final Rung element = _items.removeAt(oldIndex);
_items.insert(newIndex, element);
});
}
List<Widget> _getListItems() => _items
.asMap()
.map((i, item) => MapEntry(i, _buildTenableListTile(item, i)))
.values
.toList();
Widget _buildTenableListTile(Rung item, int index) {
return Dismissible(
key: Key(item.rungId),
onDismissed: (direction) {
setState(() {
_items.removeAt(index);
_removeditems.add(item);
});
},
background: Container(color: Colors.red),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Card(child: cardWithInfoPage(item, context, index)),
),
);
}
答案1
得分: 1
我用 "key: UniqueKey()" 替换了我的 Dismissible widget 的键,从而解决了这个问题。
英文:
I replaced the key with key: UniqueKey() to my Dismissible widget and that solved this problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论