[英]Flutter: a dismissed dismissible widget is still part of the tree
I am building a fitness app (image here: Fitness App example ) where the user can log their sets.我正在构建一个健身应用程序(这里的图片:健身应用程序示例),用户可以在其中记录他们的设置。 I am having an issue when using the dismissible widget inside of my app.
在我的应用程序中使用可关闭小部件时遇到问题。 The swipe to delete functionality sends the following exception: a dismissed dismissible widget is still part of the tree
滑动删除功能会发送以下异常:已关闭的可关闭小部件仍然是树的一部分
When swiping to delete a single set, I still need to retain the information the user has put into the other sets.当滑动删除单个集合时,我仍然需要保留用户放入其他集合的信息。 I believe this is an issue with the key, however I've already tried UniqueKey() (which resets all of the other input fields) and the example below.
我相信这是密钥的问题,但是我已经尝试过 UniqueKey()(它会重置所有其他输入字段)和下面的示例。
How can I remove a single set using dismissible and still retain the rest of the users data for the other sets?如何使用dismissible删除单个集合并仍然保留其他集合的其余用户数据? Thanks.
谢谢。
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)),
],
),
);
},
),
Since the Key is based on a list of ints, maybe there are repeated keys?由于 Key 基于整数列表,所以可能有重复的键? In that case the framework won't know which item was removed and will trigger the error you just found.
在这种情况下,框架将不知道删除了哪个项目,并会触发您刚刚发现的错误。
A possible solution would be to assign an unique ID to each item, that way you will never have repeated keys.一个可能的解决方案是为每个项目分配一个唯一的 ID,这样你就永远不会有重复的键。
尝试用UniqueKey()
替换key: ValueKey(_count[index])
UniqueKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.