Ama*_*ary 6 dart firebase flutter google-cloud-firestore
我正在返回 astreamBuilder并且在 streamBuider 内部,它返回一个小部件。
现在我已经用dismissible 包装了一个小部件,以便我可以从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,
),
);
},
);
}
Run Code Online (Sandbox Code Playgroud)
我试过这个从 cloud_firestore 中删除数据
key: ObjectKey(snapshot.data.docs.elementAt(0)),
onDismissed: (direction) {
firestoreInstance
.collection("eventDetails")
.doc()
.delete()
.then((_) {
print("success!");
});
},
Run Code Online (Sandbox Code Playgroud)
我想从集合中删除特定文档,
我不知道该怎么做。
这是数据库模型

我正在尝试删除eventDetails收藏文档。
doc() 将生成一个新的随机 id,因此如果您无权访问该 id,则需要执行以下操作:
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!");
});
});
});
Run Code Online (Sandbox Code Playgroud)
使用where()条件获取所需文档并将其删除。
由于在您的代码中您使用的是:
return StreamBuilder<QuerySnapshot>(
stream: firestoreInstance.collection('eventDetails').snapshots(),
Run Code Online (Sandbox Code Playgroud)
在这里,您正在获取 下的所有文档eventDetails,因此您可以向文档添加一个唯一字段,然后在 for 循环内您可以获得 id:
for (var i in allDocuments) {
if(i.data()["subject"] == "Mathemtics")
docId = i.id;
Run Code Online (Sandbox Code Playgroud)
然后你可以删除它:
onDismissed: (direction) {
FirebaseFirestore.instance
.collection("eventDetails")
.doc(docId)
.delete()
.then((_) {
print("success!");
});
},
Run Code Online (Sandbox Code Playgroud)
这样您就不需要两次获取文档。
| 归档时间: |
|
| 查看次数: |
3443 次 |
| 最近记录: |