Home > OS >  Flutter: a dismissed dismissible widget is still part of the tree
Flutter: a dismissed dismissible widget is still part of the tree

Time:10-12

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)),
                      ],
                    ),
                  );
                },
              ),

CodePudding user response:

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.

CodePudding user response:

Try replace 'key: ValueKey(_count[index])' with 'UniqueKey()'

  • Related