Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "A dismissed Dismissible widget is still part of the tree." error in flutter

Tags:

flutter

I am building a fluter app with dismissible widget, firebase and StreamBuilder and getting following error "A dismissed Dismissible widget is still part of the tree."

Please find the below code sniped for the same.

Expanded(
                  child: StreamBuilder(
                    stream: Firestore.instance
                        .document('/users/User1/Trips/${widget.tripId}')
                        .collection('TropDocs')
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (!snapshot.hasData) return const Text("Loading....");
                      return ListView.builder(
                        itemExtent: 150.0,
                        itemCount: snapshot.data.documents.length,
                        itemBuilder: (context, index) {

final item = snapshot.data.documents[index];
                          final itemID =
                              snapshot.data.documents[index].documentID;
                          final list = snapshot.data.documents;      
return Dismissible(
   key: Key(itemID),
              // We also need to provide a function that tells our app
              // what to do after an item has been swiped away.
              onDismissed: (direction) {
                // Remove the item from our data source.

                //fBtDoc.deleteTraveldoc(item);
                //Firestore.instance.collection('/users/User1/Trips/${widget.tripId}/TropDocs/').document('$itemID').delete();
                setState(() {
                  list.removeAt(index);
                });

                // Then show a snackbar!
                Scaffold.of(context)
                    .showSnackBar(SnackBar(content: Text("$item dismissed")));
              },
              // Show a red background as the item is swiped away
              background: Container(color: Colors.red),
              child: _buildlistitem(
                            context, snapshot.data.documents[index])
);

                        }
                      );
                    },
                  ),
                )
like image 228
Samiran Majumder Avatar asked Apr 22 '19 09:04

Samiran Majumder


3 Answers

Probably, I'm late, but I think it will be helpful for others. Had the same problem and even setting

key: Key(itemId[index]),

didn't work. However,

key: UniqueKey(),

worked perfectly for me

like image 112
Asylniet Avatar answered Nov 13 '22 09:11

Asylniet


I think that's because you are trying to use same key for every dismissible.

key: Key(itemID)

It should be key: Key(itemID[index])

like image 12
Darshan Avatar answered Nov 13 '22 07:11

Darshan


In my cause, I have used as below

key: Key(index),

and then I did change it as below. It's working

key: UniqueKey(),
like image 11
user112380 Avatar answered Nov 13 '22 08:11

user112380