Query Collection from Specific Document Id Tied To A UserId in Firebase.

Database & APIs

Hi there,

I'm building a chat interface and I'm having trouble retrieving the messages which are stored in firebase. I want to retrieve collection "messages" from a specific {chatId} and {userId}. I had no problem retrieving all the messages with a database query when had all the messages stored under "Chats" and only had to refer to the {userId}, but now that I've added another level with multiple chatIds, each with their own collection of messages, I can't seem to retrieve the messages. When I use the database query, the parent ref is the {userId}, but I don't know how to specify which {chatId} I want to retrieve. All the messages are then displayed in a list view, with the "sender" field determining how it gets displayed.

Below is my firestore database structure.

users (Collection)

└── {userId} (Document)

β”œβ”€β”€ Chats (Sub Collection)

β”‚ └── {chatId} (Document)

β”‚ β”œβ”€β”€ title (Field - String)

β”‚ β”œβ”€β”€ createdAt (Field - Date)

β”‚ └── lastMessageAt (Field - Date)

β”‚ └── messages (Sub Collection)

β”‚ └── {messageId} (Document)

β”‚ β”œβ”€β”€ msg (field - string)

β”‚ β”œβ”€β”€ sender (field - string)

β”‚ β”œβ”€β”€ timestamp (field - date)

β”‚ β”œβ”€β”€ bookmark (field - boolean)

Any help is greatly appreciated.

What have you tried so far?

1 - Tried using filters to get the correct chatId, but that doesn't work because the filter only references fields and not documents.

2. Tried a custom action and a custom function to retrieve the documents (messages), but that does't seem to work. I need to be able to set the "msg" field to my text field, and use the "sender" field in the conditional formating so that the messages display correctly. I also have a bookmark button which needs to toggle between true/false when a user clicks the button, so I believe the custom action and functions aren't the solution.

Did you check FlutterFlow's Documentation for this topic?
Yes
1
3 replies