0

I'm currently making a Flutter app for practice, similar to a Twitter clone app. However, I have a problem when I try to get all documents from Firestore Database. I use UID as document id and want to see all user's posts. But only the posts of the currently logged in user are displayed.

In this case, how can I get all the documents?

I want to display posts by user [B1vq9.........] and posts by [QVf5.........].

image

I tried:

final FirebaseAuth auth = FirebaseAuth.instance; final User? user = auth.currentUser; final uid = user!.uid; FirebaseFirestore.collection('post').doc(uid).collection('talk').snapshots(); 

However, this code can only display posts from the currently logged in user.

collection('talk') is a subcollection.enter image description here

1 Answer 1

3

If you need all the posts from posts's collection you only need

FirebaseFirestore.collection('post').snapshots() 

If you append .doc(uid).collection('talk') then this will fetch all the documents inside {uid}/talk.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi. Thank you for your advice and I tried it. But this code above [FirebaseFirestore.collection('post').snapshots()]couldn't get all user's posts. I also tried [FirebaseFirestore.collection('post').doc(uid).collection('talk').snapshots()] which can access only current logged in user's post.
You mean to say each user's post is inside post/{uid}/talk path?
Hi. Thanks for your reply. Yes each user's post is inside of post/{uid}/talk.
if that's the case, you might have to do a loop on FirebaseFirestore.collection('post') for each document and get the documents inside 'talk' collection. I would suggest put the posts inside the 'posts' collection directly and add a 'createdBy' field with the user id of the respective user.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.