-1

I have a streambuilder, I thought I understood how it worked but now i am not so sure. why does this work =>

                      FirebaseFirestore.instance
                      .collection('messages')
                      .doc(widget.currentUser)
                      .collection('messages')
                      .doc(widget.otherUser)
                      .collection('messages')
                      .snapshots() 

the above stream returns results. the documents inside the collection are custom ids not auto-generated from firestore on the other hand this does not work =>

                      FirebaseFirestore.instance
                      .collection('messages')
                      .doc(widget.currentUser)
                      .collection('messages')
                      .snapshots()

is there a reason why it doesn't return the results? the documents inside the above collection are firebase firestore auto-generated ids

and waojd
  • 35
  • 5

1 Answers1

0

tl;dr: read operations in Firestore are shallow, so reading a document from a collection does not include the subcollections of that document. You will have to perform separate read operations for each subcollection.


It sounds like you have a structure like this:

messages
  $uid1
    messages
      $uid2
        messages
          $message documents are here

Your first statement reads the message documents from the lowest subcollection: /messages/$uid1/messages/$uid2/messages.

Your second statement tries to read documents from /messages/$uid1/messages and apparently there are no documents in that collection.


If you want to read across all $uid2/messages for a specific /messages/$uid1 path, you can consider the approach sam showed here: CollectionGroupQuery but limit search to subcollections under a particular document

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Yeah, apparently there are no documents, but there is, for some reason when i try to get those documents, if they are set using a custom given id that i gave to the documents, they work, but if it uses firebase auto generated id, they do not exist, unless you go to the lowest subcollection, i do not want to go to the lowest subcollection. – and waojd Dec 23 '22 at 10:34
  • Telling us what you don't want to do is typically not the most helpful. My answer is the best explanation I could give based on what you showed in your answer. If you're still stuck, I recommend showing more debugging output in your question and including screenshots from the Firestore console of the documents you expect to get back. It's more likely we can explain the behavior based on that then on the description so far. – Frank van Puffelen Dec 23 '22 at 16:07
  • I added a random field to the documents, they now exist, apparently documents with firebase auto-generated id do not exist unless they have a at least one field. please read the question before answering again, thanks. – and waojd Dec 24 '22 at 13:22