Как устранить ошибку «Тип возврата DocumentReference ()» не является ошибкой «DocumentReference ()», как определено методом »? - PullRequest
0 голосов
/ 28 марта 2019

У меня есть 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
    }

1 Ответ

0 голосов
/ 28 марта 2019

Я думаю, что проблема в том, что вы не ждете своего 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;
    }

Вот отличная статья о будущем и асинхронности: Ссылка на статью

Надеюсь, это поможет !!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...