![](/img/trans.png)
[英]A dismissed Dismissible widget is still part of the tree in flutter
[英]Flutter: a dismissed dismissible widget is still part of the tree
我正在构建一个健身应用程序(这里的图片:健身应用程序示例),用户可以在其中记录他们的设置。 在我的应用程序中使用可关闭小部件时遇到问题。 滑动删除功能会发送以下异常:已关闭的可关闭小部件仍然是树的一部分
当滑动删除单个集合时,我仍然需要保留用户放入其他集合的信息。 我相信这是密钥的问题,但是我已经尝试过 UniqueKey()(它会重置所有其他输入字段)和下面的示例。
如何使用dismissible删除单个集合并仍然保留其他集合的其余用户数据? 谢谢。
late List count = [0];
ListView.builder(
shrinkWrap: true,
itemCount: _count.length,
itemBuilder: (context, index) {
// Create a new variable to display the set
int setNumber = index + 1;
return Dismissible(
key: ValueKey(_count[index]),
background: _swipeStyle(),
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
_count.removeAt(index);
});
},
child: Row(
children: [
Expanded(flex: 1, child: Text('Set $setNumber')),
Expanded(flex: 2, child: _buildWeight(index)),
const SizedBox(
width: 24.0,
),
Expanded(flex: 2, child: _buildReps(index)),
],
),
);
},
),
由于 Key 基于整数列表,所以可能有重复的键? 在这种情况下,框架将不知道删除了哪个项目,并会触发您刚刚发现的错误。
一个可能的解决方案是为每个项目分配一个唯一的 ID,这样你就永远不会有重复的键。
尝试用UniqueKey()
替换key: ValueKey(_count[index])
UniqueKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.