英文:
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 'package:cloud_firestore/cloud_firestore.dart';
Future<List<ChatsRecord>> 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('chats'); // Get a reference to the collection final filteredDocuments = collectionRef.where('users', 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<ChatsRecord> 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: 'ChatsRecord.fromMap'.
ChatsRecord chatDoc = ChatsRecord.fromMap(doc.data);
^^^^^^^
lib/custom_code/actions/get_chat_doc.dart:33:47:
Error: The getter 'documents' isn't defined for the class 'QuerySnapshot<Map<String, dynamic>>'.
- 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/root/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-4.2.0/lib/cloud_firestore.dart').
- 'Map' is from 'dart:core'.
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论