Firebase 如何检查cloud firestore中是否存在用于Flatter的文档?
我想使用快照检查firestore中的文档是否存在,当我尝试获取一个不存在的文档时,收到一个返回null的红屏错误Firebase 如何检查cloud firestore中是否存在用于Flatter的文档?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我想使用快照检查firestore中的文档是否存在,当我尝试获取一个不存在的文档时,收到一个返回null的红屏错误 StreamBuilder( stream: Firestore.instance .collection('Resumes') .document(uid) .snapshots(), builder: (contex
StreamBuilder(
stream: Firestore.instance
.collection('Resumes')
.document(uid)
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
};
如何检查名为uid的文档是否存在?请执行以下操作,而不是文档(“uid”):
Firestore.instance .collection('Resumes').documents;
然后可以遍历文档,检查是否与documentId属性匹配
final document = snapshot.data.firstWhere((doc) => doc.documentId = uid, orElse: () => null)
然后,检查是否存在文档!=null而不是文档('uid')执行以下操作:
Firestore.instance .collection('Resumes').documents;
var res=Firestore.instance .collection('Resumes').documents;
if(res==null){
print("no documents found");
}else{
//there are documents
}
然后可以遍历文档,检查是否与documentId属性匹配
final document = snapshot.data.firstWhere((doc) => doc.documentId = uid, orElse: () => null)
然后,检查是否存在文档!=空的
var res=Firestore.instance .collection('Resumes').documents;
if(res==null){
print("no documents found");
}else{
//there are documents
}
将集合实例设置为变量,并检查该变量是否为null或是否使用if语句
将集合实例设置为变量,并使用if语句检查该变量是否为null。您可以使用文档快照上的方法检查文档是否存在:
DocumentSnapshot docSnapshot = await Firestore.instance.collection('Resumes').document(uid).get();
bool isDocExists = docSnapshot.exists(); //true if exists and false otherwise
您可以使用文档快照上的方法检查文档是否存在:
DocumentSnapshot docSnapshot = await Firestore.instance.collection('Resumes').document(uid).get();
bool isDocExists = docSnapshot.exists(); //true if exists and false otherwise
snapshot.hasData
返回true
,即使集合中不存在指定的文档
要检查文档是否存在,可以检查snapshot.data.data==null
因此,您必须将代码更改为:
StreamBuilder(
stream: Firestore.instance
.collection('Resumes')
.document(uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if(snapshot.data.data==null){
//document does not exist
}
else{
//document exists
}
} else{
//Waiting for data
return Center(child: CircularProgressIndicator());
}
};
snapshot.hasData
返回true
,即使集合中不存在指定的文档
要检查文档是否存在,可以检查snapshot.data.data==null
因此,您必须将代码更改为:
StreamBuilder(
stream: Firestore.instance
.collection('Resumes')
.document(uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if(snapshot.data.data==null){
//document does not exist
}
else{
//document exists
}
} else{
//Waiting for data
return Center(child: CircularProgressIndicator());
}
};
你得到的错误是什么虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题会真正有助于提高你的文章质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。虽然此代码可能会解决此问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。