[英]How to solve “The return type 'DocumentReference ()' isn't a 'DocumentReference ()', as defined by the method” error?
我有3個函數, doesNameAlreadyExist
檢查文檔是否存在。 順便說一句,我對這些方法的改進持開放態度。
Future<bool> doesNameAlreadyExist(String name) async {
QuerySnapshot queryDb = await Firestore.instance
.collection('locations')
.where("city", isEqualTo: '${name}')
.limit(1)
.getDocuments();
final List<DocumentSnapshot> documents = queryDb.documents;
return documents.length == 1;
// I have to return DocumentReference if document is exists,
// Even though, it's not on the scope of this particular problem,
// I'm open to ideas. Maybe I can return a map, bool and reference combined
}
這個在firestore上推文件。
Future<DocumentReference> pushNameToFirestore(PlaceDetails pd) async {
Future<DocumentReference> justAddedRef = Firestore.instance.collection('locations').add(<String, String>{
'city': '${pd.name}',
'image': '${buildPhotoURL(pd.photos[0].photoReference)}',
});
return justAddedRef;
}
在這里,我正在檢查,然后推動上述功能。 但是我無法返回文檔參考。
DocumentReference firestoreCheckAndPush() async {
bool nameExists = await doesNameAlreadyExist(placeDetail.name);
// TODO notify user with snackbar and return reference
DocumentReference locationDocumentRef;
if(nameExists) {
print('name exist');
} else {
locationDocumentRef = await pushNameToFirestore(placeDetail);
}
return locationDocumentRef; // Error is here
}
我認為這里的問題是你不等待你的DocumentReference
,所以你返回Future
而不是實際預期的DocumentReference
。
嘗試這個:
Future<DocumentReference> pushNameToFirestore(PlaceDetails pd) async {
DocumentReference justAddedRef = await Firestore.instance.collection('locations').add(<String, String>{
'city': '${pd.name}',
'image': '${buildPhotoURL(pd.photos[0].photoReference)}',
});
return justAddedRef;
}
這是一篇關於未來和異步的精彩文章: 鏈接到文章
希望它的幫助!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.