- 我的
eventClick
表中有两个属性eventID
和userID
所以我想要做的是如果eventID
code> 和userID
通常存在,然后显示您已经点击
,如果不存在,则输入该点击。我想过使用两个子句并将其合并,但这只会导致插入值。 - 我正在尝试使用以下代码
final checkSnapshot = FirebaseFirestore.instance
.collection('eventClick')
.where('eventID', isEqualTo: eventID)
.where('userID', isEqualTo: userID)
.snapshots();
- 我希望工作是这样的
if (checkSnapshot.exists) {
print('already exists');
} else {
FirebaseFirestore.instance.collection('eventClick').add({
'eventID': eventID,
'eventName': eventName,
'eventImageUrl': eventImageUrl,
'userID': userID
});
}
最佳答案
你可以尝试使用 .get() ?这应该返回 QuerySnapshot它具有“大小”属性。如果为0,则表示不存在这样的文档。
FirebaseFirestore.instance
.collection('eventClick')
.where('eventID', isEqualTo: eventID)
.where('userID', isEqualTo: userID)
.get()
.then((checkSnapshot) {
print(checkSnapshot.docs[0]);
if (checkSnapshot.size > 0) {
print("Already Exists");
} else {
//add the document
}
});
关于firebase - 在flutter中使用2个where子句查询Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68191641/