【发布时间】:2019-08-19 05:46:10
【问题描述】:
我的州有DocumentReference locationDocumentRef;。
我正在根据引用更改locationDocumentRef,无论是通过查询还是通过添加新文档来收集。
所以我有这个功能来检查文件,如果有一个设置其引用locationDocumentRef,或者添加一个新的并将其引用设置为locationDocumentRef。我每次都通过将其设置为 null 来重置它的值,因为我不想获得以前的结果。但它打印空。
所以我的问题是,我怎样才能解决它们并获得价值?我认为我在代码中解决得太早了,所以我不能等待一个非未来的值。我该如何解决?
void firestoreCheckAndPush() async {
setState(() {
locationDocumentRef = null;
});
bool nameExists = await doesNameAlreadyExist(placeDetail.name);
if (nameExists) {
print('name exist');
} else {
print('name will be pushed on firestore');
pushNameToFirestore(placeDetail);
}
var resolvedRef = await locationDocumentRef;
print(resolvedRef.documentID); // I get null here
}
这些是我用过的功能
Future<bool> doesNameAlreadyExist(String name) async {
QuerySnapshot queryDb = await Firestore.instance
.collection('locations')
.where("city", isEqualTo: '${name}')
.limit(1)
.getDocuments();
if (queryDb.documents.length == 1) {
setState(() {
locationDocumentRef = queryDb.documents[0].reference;
});
return true;
} else {
return false;
}
}
还有一个
void pushNameToFirestore(PlaceDetails pd) async {
DocumentReference justAddedRef =
await Firestore.instance.collection('locations').add(<String, String>{
'city': '${pd.name}',
'image': '${buildPhotoURL(pd.photos[0].photoReference)}',
});
setState(() {
locationDocumentRef = justAddedRef;
});
}
【问题讨论】:
标签: dart flutter async-await google-cloud-firestore