@override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(widget.uid)
.collection('lists')
.snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot data = snapshot.data.documents[index];
final list = snapshot.data.documents;
return Dismissible(
// Dismissed function
key: UniqueKey(),
onDismissed: (DismissDirection direction) {
setState(() {
// This is where I would like to remove the element
// snapshot.data.remove(index);
});
},
secondaryBackground: Container(
child: Center(
child: Text('Delete',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.start),
),
color: Colors.red,
),
background: Container(),
child: Card(
child: Center(
child: new Container(
padding: new EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[
new Text(data['listName']),
new Text(data['Description'])
],
),
),
),
),
direction: DismissDirection.endToStart,
);
},
);
},
),
Any help is appreciated and hopefully, we can solve this together :)
Thank you in advance!
Get the document reference first by doing this:
Iterate over this: snapshot.data.documents
then snapshot.data.documents[i].documentID
then this:
onDismissed: (direction) async {
await Firestore.instance.runTransaction(
(transaction) async {
await transaction.delete(your doc ref here);
},
);
},