简体   繁体   English

Flutter:已关闭的可关闭小部件仍然是树的一部分

[英]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.

相关问题 已关闭的 Dismissible 小部件仍然是 flutter 中树的一部分 - A dismissed Dismissible widget is still part of the tree in flutter 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 在 flutter - How to fix 'A dismissed Dismissible widget is still part of the tree.' in flutter 已关闭的 Dismissible 小部件仍然是树的一部分。 在 flutter - A dismissed Dismissible widget is still part of the tree. In flutter 被关闭的 Dismissible 小部件仍然是树的一部分 - A dismissed Dismissible widget is still part of the tree Flutter Dismissed Dismissible 小部件仍然是树问题的一部分,无法解决 - Flutter Dismissed Dismissible widget still part of tree issue, can't resolve 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” flutter 错误 - How to fix "A dismissed Dismissible widget is still part of the tree." error in flutter 如何解决问题:使用 Bloc 时“已关闭的 Dismissible 小部件仍然是树的一部分” - How to resolve issue: “A dismissed Dismissible widget is still part of the tree” while use Bloc 已关闭的 Dismissible 小部件仍然是树的一部分。 从列表中删除项目后重复出现此错误 - A dismissed Dismissible widget is still part of the tree. Getting this error repeatedly after removing item from list `一个被解雇的 Dismissible 小部件仍然是树的一部分`错误突然发生(FutureBuilder,ListView) - `A dismissed Dismissible widget is still part of the tree` error occurred suddenly (FutureBuilder, ListView) 在 Flutter 中,是否可以增加 Dismissible 小部件的透明度? - In Flutter is it possible to increase the transparency of a Dismissible widget the further it is dismissed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM