error trying to query firestore documents using dart

huangapple go评论70阅读模式
英文:

error trying to query firestore documents using dart

问题

以下是您的代码的翻译部分:

我想要查询Firestore中包含两个特定元素(顺序无关紧要)的字段的某个记录。

然而,错误显示我无法从此查询返回值。具体来说,getter 'documents' 对于映射不可用。具有讽刺意味的是,我创建了这个映射,因为我需要返回一个不是Snapshot的列表

这是我的代码:
```dart
import 'package:cloud_firestore/cloud_firestore.dart';

Future<List<ChatsRecord>> getChatDoc(DocumentReference chatUserRef, DocumentReference authUserRef) async {
  final firestore = FirebaseFirestore.instance;
  final collectionRef = firestore.collection('chats');
  final filteredDocuments = collectionRef.where('users', isEqualTo: [
    authUserRef,
    chatUserRef,
  ]);

  final queryDocuments = await filteredDocuments.get();

  List<ChatsRecord> listChatDocs = [];

  for (DocumentSnapshot doc in queryDocuments.docs) {
    ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data());
    listChatDocs.add(chatDoc);
  }

  return listChatDocs;
}

这是错误信息:

错误:命令失败:flutter build web --web-renderer html --no-pub --no-version-check
目标dart2js失败:异常:
lib/custom_code/actions/get_chat_doc.dart:34:39:
错误:找不到成员'ChatsRecord.fromMap'。
ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
^^^^^^^
lib/custom_code/actions/get_chat_doc.dart:33:47:
错误:对于类'QuerySnapshot<Map<String, dynamic>>',未定义getter 'documents'。
- 'QuerySnapshot'来自'package:cloud_firestore/cloud_firestore.dart'('/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart')。
- 'Map'来自'dart:core'。
for (DocumentSnapshot doc in queryDocuments.documents) {
^^^^^^^^^
错误:编译失败。
英文:

I want to query Firestore for a certain record where the field that is a List contains two specific elements (order doesn't matter).

However, the error says that I cannot return the value from this query. Specifically the getter 'documents' is not available for a map. Ironically, I created the map because the I needed to return a list which is not a Snapshot.

This is my code:

    import &#39;package:cloud_firestore/cloud_firestore.dart&#39;;
    
    Future&lt;List&lt;ChatsRecord&gt;&gt; getChatDoc(   DocumentReference chatUserRef, DocumentReference authUserRef, ) async {   // Add your function code here!
    
      final firestore =
          FirebaseFirestore.instance; // Get a reference to the Firestore database   final collectionRef =
          firestore.collection(&#39;chats&#39;); // Get a reference to the collection   final filteredDocuments = collectionRef.where(&#39;users&#39;, isEqualTo: [
        authUserRef,
        chatUserRef   ]); // Use the `where` method to filter the list of documents
    
      final queryDocuments = await filteredDocuments
          .get(); // You can then use the get method on this Query object to retrieve the list of documents.
    
      List&lt;ChatsRecord&gt; listChatDocs = [];
    
      for (DocumentSnapshot doc in queryDocuments.documents) {
        ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
        listChatDocs.add(chatDoc); // add chatDoc
    
      }
    
      return listChatDocs; }

Here is the error:

Error: Command failed: flutter build web  --web-renderer html --no-pub --no-version-check
Target dart2js failed: Exception:
lib/custom_code/actions/get_chat_doc.dart:34:39:
Error: Member not found: &#39;ChatsRecord.fromMap&#39;.
    ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
                                      ^^^^^^^
lib/custom_code/actions/get_chat_doc.dart:33:47:
Error: The getter &#39;documents&#39; isn&#39;t defined for the class &#39;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&#39;.
 - &#39;QuerySnapshot&#39; is from &#39;package:cloud_firestore/cloud_firestore.dart&#39; (&#39;/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart&#39;).
 - &#39;Map&#39; is from &#39;dart:core&#39;.
  for (DocumentSnapshot doc in queryDocuments.documents) {
                                              ^^^^^^^^^
Error: Compilation failed.

答案1

得分: 1

I think is queryDocuments.docs not queryDocuments.documents.

英文:

I think is queryDocuments.docs not queryDocuments.documents

huangapple
  • 本文由 发表于 2023年1月6日 12:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定