我想查询Firestore中某个记录,其中字段是List包含两个特定元素(顺序无关紧要)。
但是,错误提示我不能返回这个查询的值。特别是getter 'documents'对于map是不可用的。具有讽刺意味的是,我创建映射是因为我需要返回一个列表,而这个列表不是快照。
这是我的代码:
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; }
错误如下:
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.
我认为是queryDocuments.docs
而不是queryDocuments.documents