У меня есть 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
}
Этот документ помещает в пожарный магазин.
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
}