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])
);
}
);
},
),
)
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
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])
In my cause, I have used as below
key: Key(index),
and then I did change it as below. It's working
key: UniqueKey(),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With