satchel
satchel

Reputation: 547

How to get the List of Document Reference from a Snapshot Document resulting from a Firestore query

I can't seem to figure out how to get a List after querying a specific Firestore collection.

I want the function to:

This is what I am trying:

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 AS a Snapshot Document (this is NOT a list of the Documents themselves).

  List<ChatsRecord> listChatDocs = [];

  // Cast the Snapshot Documents to a map
  // Extract the Document Reference ID
  // cast the query document to a map for e
  // (should I forEach?)
  List<ChatsRecord> listChatDocs  = queryDocuments.docs.map((e) {
          return FirebaseFirestore.instance.doc('chats/$e.id');
        }).toList();

  return listChatDocs;
}

Upvotes: 0

Views: 464

Answers (1)

jorge
jorge

Reputation: 72

Try using the arrayContainsAny instead of EqualTo in your where clause. Like this:

final filteredDocuments = collectionRef.where('users', arrayContainsAny: [
authUserRef,
chatUserRef
]); 

Upvotes: 0

Related Questions