Reputation: 912
I am returning a streamBuilder
and inside the streamBuider, it returns a widget.
Now I have wrap a widget with dismissible so that I can delete the document from the collection from the cloud_firestore.
showingTheSelectedDateEvents() {
List<Widget> listViewContainer = [];
return StreamBuilder<QuerySnapshot>(
stream: firestoreInstance.collection('eventDetails').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
String theDatabaseDate;
final allDocuments = snapshot.data.docs;
//here we get all the documents from the snapshot.
for (var i in allDocuments) {
theDatabaseDate = i.data()['dateTime'];
if (theDatabaseDate == theDataProvider.databaseSelectedDate) {
print(theDatabaseDate +
" is same as " +
theDataProvider.databaseSelectedDate);
listViewContainer.add(Dismissible(
key: ObjectKey(snapshot.data.docs.elementAt(0)),
onDismissed: (direction) {
firestoreInstance
.collection("eventDetails")
.doc()
.delete()
.then((_) {
print("success!");
});
},
child://here
));
print(listViewContainer.length);
} else {
print("no any events for today");
}
}
return Expanded(
child: ListView(
reverse: true,
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
children: listViewContainer,
),
);
},
);
}
I tried this for deleting the data from the cloud_firestore
key: ObjectKey(snapshot.data.docs.elementAt(0)),
onDismissed: (direction) {
firestoreInstance
.collection("eventDetails")
.doc()
.delete()
.then((_) {
print("success!");
});
},
I want to delete the specific document from the collection
I cannot figure out how to do that.
here is the database model
I am trying to delete the document of eventDetails
collection.
Upvotes: 6
Views: 8999
Reputation: 267404
You can always query the document to get the document ID and perform the deletion.
var collection = FirebaseFirestore.instance.collection('users');
var snapshot = await collection.where('age', isGreaterThan: 20).get();
for (var doc in snapshot.docs) {
await doc.reference.delete();
}
To delete all the documents, iterate through the QueryDocumentSnapshot
.
var collection = FirebaseFirestore.instance.collection('collection');
var querySnapshots = await collection.get();
for (var doc in querySnapshots.docs) {
await doc.reference.delete();
}
Upvotes: 1
Reputation: 912
I just had to use,
i.id
to get the document id. And I could delete the document from the firestore by swipping.
Here is the complete code,
showingTheSelectedDateEvents() {
List<Widget> listViewContainer = [];
return StreamBuilder<QuerySnapshot>(
stream: firestoreInstance.collection('eventDetails').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
String theDatabaseDate;
final allDocuments = snapshot.data.docs;
//here we get all the documents from the snapshot.
for (var i in allDocuments) {
// if(i.data()["subject"] == "Mathemtics")
theDatabaseDate = i.data()['dateTime'];
if (theDatabaseDate == theDataProvider.databaseSelectedDate) {
print(theDatabaseDate +
" is same as " +
theDataProvider.databaseSelectedDate);
listViewContainer.add(Dismissible(
key: ObjectKey(snapshot.data.docs.elementAt(0)),
onDismissed: (direction) async {
firestoreInstance.collection("eventDetails").doc(i.id).delete();
// allDocuments.removeAt(0);
},
background: Container(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
child: Text(
"Slide To Remove",
style: TextStyle(
fontSize: 15,
fontFamily: "Poppins",
color: Colors.black),
),
),
decoration: BoxDecoration(color: Color(0xFFF4F5F6)),
),
child: Widgets(
containerHeight: 170,
thePaddingValue:
EdgeInsets.only(left: 90, right: 10, top: 20, bottom: 20),
iconSize: 60,
chapterName: i.data()['chapterName'],
chapterNumber: i.data()['chapterNumber'],
meetType: i.data()['meetType'],
meetIcon: Icons.video_call,
subject: i.data()['subject'],
lectureTime: "09:30",
teacherImage:
AssetImage("assets/images/IMG-20200817-WA0000.jpg"),
teacherName: "Alex Jesus",
),
));
print(listViewContainer.length);
} else {
print("no any events for today");
}
}
return Expanded(
child: ListView(
reverse: true,
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
children: listViewContainer,
),
);
},
);
}
Upvotes: 0
Reputation: 80904
doc()
will generate a new random id, therefore if you don't have access to the id, then you need to do the following:
FirebaseFirestore.instance
.collection("eventDetails")
.where("chapterNumber", isEqualTo : "121 ")
.get().then((value){
value.docs.forEach((element) {
FirebaseFirestore.instance.collection("eventDetails").doc(element.id).delete().then((value){
print("Success!");
});
});
});
Use a where()
condition to get the required document and delete it.
Since in your code you are using:
return StreamBuilder<QuerySnapshot>(
stream: firestoreInstance.collection('eventDetails').snapshots(),
Here you are fetching all the documents under eventDetails
, therefore you can add a unique field to the document and then inside the for loop you can get the id:
for (var i in allDocuments) {
if(i.data()["subject"] == "Mathemtics")
docId = i.id;
And then you can delete it:
onDismissed: (direction) {
FirebaseFirestore.instance
.collection("eventDetails")
.doc(docId)
.delete()
.then((_) {
print("success!");
});
},
This way you dont need to fetch the documents twice.
Upvotes: 13