簡體   English   中英

Flutter FireStore:未為類型“DocumentReference”定義方法“where”

[英]Flutter FireStore: The method 'where' isn't defined for the type 'DocumentReference'

如果我使用代碼,我可以正確接收數據。 但是,使用.where時,出現錯誤:

沒有為類型“DocumentReference”定義方法“where”。 嘗試將名稱更正為現有方法的名稱,或定義名為“where”的方法。

代碼:

chamaBancoDeDados(curso, materia, id, dificuldade) {
    Map<String, dynamic> dados = {};
    final documentoRef = FirebaseFirestore.instance
        .collection('cursos')
        .doc(curso)
        .collection(materia)
        .doc(id)
        .where('dificuldade', arrayContainsAny: ['normal']);
    documentoRef
        .get()
        .then((DocumentSnapshot documento) {
      if (documento.exists) {
        try {
          dados = documento.data() as Map<String, dynamic>;
          print("dados são: $dados");
        } on StateError catch (e) {
          print('Erro tentando adquirir o documento no banco de dados: $e');
        }
      } else {
        print('O documento não existe no banco de dados');
      }
    });
    return dados;
  }
}

我已經在 inte.net 上搜索了很多,包括 Firebase 文檔,但我不知道哪里出錯了。

如果您在代碼的line 7看到,它從 firestore 獲取文檔,並且您不能在文檔上使用.where()

.where()用於 collections 檢查所有文檔(在該集合中)的條件並返回滿足需要的文檔。

根據上面給出的信息,我建議您刪除7th line代碼並重試。

請分享您的 Firestore 層次結構,以便我們建議更好的方法來執行您的查詢。

您當前的代碼嘗試讀取具有已知 ID 的單個文檔,在這種情況下,無法使用where子句進一步過濾數據。 因此,如果您只想閱讀該單個文檔,請刪除where

final documentoRef = FirebaseFirestore.instance
    .collection('cursos')
    .doc(curso)
    .collection(materia)
    .doc(id);

另一方面,如果您想以正常難度級別讀取 `` 集合中的所有文檔,可以通過刪除.doc(id)子句來完成:

final documentoRef = FirebaseFirestore.instance
    .collection('cursos')
    .doc(curso)
    .collection(materia)
    .where('dificuldade', arrayContainsAny: ['normal']);

我做了這個並按我想要的方式工作:

    final teste = FirebaseFirestore.instance
    .collection('cursos')
    .doc(curso)
    .collection(materia)
    .where('dificuldade', arrayContainsAny: [dificuldade]); 
await teste
    .get()
    .then((event) {
    print("${doc.id} => ${doc.data()}");
  }
}, onError: (e) => print("Error completing: $e"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM