Liv*_*o98 3 dart firebase flutter google-cloud-firestore
我尝试使用 bool 查询 cloudfirestore 上是否存在文档。不幸的是,我的代码不起作用
我尝试了以下操作,但 bool 没有改变。
getok() {
bool ok;
Firestore.instance.document('collection/$name').get().then((onexist){
onexist.exists ? ok = true : ok = false;
}
);
if (ok = true) {
print('exist');
} else {
print('Error');
}
}
Run Code Online (Sandbox Code Playgroud)
G g*_*ffo 21
你可以尝试这样做虽然我使用IDS
//Declare as global variable
bool exist;
static Future<bool> checkExist(String docID) async {
try {
await Firestore.instance.document("users/$docID").get().then((doc) {
exist = doc.exists;
});
return exist;
} catch (e) {
// If any error
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*out 12
您可以调用一个简单的 async / await 函数来检查文档是否存在。返回真或假。
bool docExists = await checkIfDocExists('document_id');
print("Document exists in Firestore? " + docExists.toString());
/// Check If Document Exists
Future<bool> checkIfDocExists(String docId) async {
try {
// Get reference to Firestore collection
var collectionRef = Firestore.instance.collection('collectionName');
var doc = await collectionRef.document(docId).get();
return doc.exists;
} catch (e) {
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以试试这个,它对我有用
Future getDoc() async{
var a = await Firestore.instance.collection('collection').document($name).get();
if(a.exists){
print('Exists');
return a;
}
if(!a.exists){
print('Not exists');
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9385 次 |
| 最近记录: |