简体   繁体   中英

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.

How can I remove a single set using dismissible and still retain the rest of the users data for the other sets? 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? 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.

尝试用UniqueKey()替换key: ValueKey(_count[index]) UniqueKey()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM