-1

My Firebase Form like this;

  • Users

    • User1 ID

      - Posts
      
    • User2 ID

      - Posts
      

The number of users changes according to the number of users in the application.

What I am trying to do is to show the posts of the users I have selected on my home screen.

So first of all I created a list like this(Users I want to show their posts);

List<dynamic> userIDs = [
    "User1ID",
    "User2ID"
  ];

Then I tried to use these elements in the list in a reference inside a for loop, Because I tried to show more than one user's post.

This is CollectionReference in for loop;

final firestore = FirebaseFirestore.instance;

var userPostsRef;

for (int i = 0; i < userIDs.length; i++) {
      userPostsRef = userPostsRef.firestore.collection('users/${userIDs[i]}/Posts');
    }

But it didn't work.

When I use CollectionReference like this;

var userPostsRef = firestore.collection('users/${userIDs[0]}/Posts');

It worked but I dont want to show single user posts, I want to show multiple users posts.

How can I show multiple users' posts on my home screen with this method or a different method?

Added StreamBuilder part;

StreamBuilder<QuerySnapshot>(
                      stream: userPostsRef,
                      builder:
                          (BuildContext context, AsyncSnapshot asyncsnapshot) {
                        if (asyncsnapshot.hasError) {
                          return Center(
                            child: Text("Error"),
                          );
                        } else {
                          if (asyncsnapshot.hasData) {
                            List<DocumentSnapshot> listOfDocumentSnapshot =
                                asyncsnapshot.data.docs;
                            return ListView.builder(
                              physics: ScrollPhysics(),
                              shrinkWrap: true,
                              itemCount: listOfDocumentSnapshot.length,
                              itemBuilder: (BuildContext context, int index) {
                                return Padding(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 12.0, vertical: 12.0),
                                  child: Container(
                                    child: Column(
                                      children: <Widget>[
                                        Stack(
                                          children: <Widget>[
                                            Align(
                                              alignment: Alignment.topCenter,
                                              child: ClipRRect(
                                                borderRadius:
                                                BorderRadius.circular(24),
                                                child: GestureDetector(
                                                  onTap: () => navigateToDetail(
                                                      listOfDocumentSnapshot[
                                                      index]),
                                                  child: Image(
                                                    height: 320,
                                                    width: 320,
                                                    fit: BoxFit.cover,
                                                    image: NetworkImage(
                                                        listOfDocumentSnapshot[
                                                        index]["photo"]),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              },
                            );
                          }
                           else {
                            return Center(
                              child: CircularProgressIndicator(
                                color: Colors.orangeAccent[400],
                              ),
                            );
                          }
                        }
                      },
                    ),
Rkdio
  • 153
  • 2
  • 13
  • The Firebase Realtime Database and Cloud Firestore are two separate databases. Please only mark your question with the relevant tag, not with both. – Frank van Puffelen Dec 15 '21 at 00:19
  • I solved my problem, you can check it from this link; [https://stackoverflow.com/questions/70407597/flutter-firebase-merge-streams-list-with-combinelateststream-and-display-on-stre/70433919#70433919](https://stackoverflow.com/questions/70407597/flutter-firebase-merge-streams-list-with-combinelateststream-and-display-on-stre) – Rkdio Dec 21 '21 at 16:06

2 Answers2

0

There is no way to load from the Posts subcollections from a list of UIDs and not from others.

The closest that Firestore supports is loading from all Posts collections with a collection group query, or from all Posts subcollections under a specific path with the trick samthecodingman showed here: CollectionGroupQuery but limit search to subcollections under a particular document

If you can't change your data model to allow getting the data from the relevant subcollections with a single query, you will have to execute multiple queries and merge the results in your application code.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • When I did the application for the first time, as a collectiongroup, I could pull all the posts and show only the posts with the specific id on the screen thanks to the whereIN query, but in the test phase, I saw that the whereIN query could only query 10 values, so the application could only display the maximum 10 posts. Because of this problem, I started looking for different solutions. – Rkdio Dec 15 '21 at 09:47
  • So in summary what i'm trying to do is only retrieve the data of specific multiple users. Is there a way to do this, in my current database form or any other form. – Rkdio Dec 15 '21 at 10:50
  • If I need to change my database form, what would you suggest I do according to this example? – Rkdio Dec 15 '21 at 11:00
  • The best data model depends on your app more than anything else, but if you can't come up with a data model you'll have to execute multiple queries and merge the results in your application code. – Frank van Puffelen Dec 15 '21 at 15:44
0

You cant use query collection reference like this

var userPostsRef = firestore.collection('users/${userIDs[0]}/Posts');

my trick I used for my app

if you want to load all post under user id I recommend to wrap future builder (post list) under future builder (user list)

the other way is set user id with something you can track like (I use firebase.auth.email for document id reference so query will be easier)

or You can get all user id reference at the start of app and put that in memory variable so you can reduce redundancy get user id reference for the rest of functionality (mind about delete/changed user etc)

the other way around is make a dynamic query based on user action (this need more work around base on ur app)

Tony Hart
  • 145
  • 1
  • 12
  • I dont have any problem with the retrieve specific single userIDs data. For example I have 5 user; user1, user2, user3, user4, user5 and I want to retrieve user2 and user3 posts or user3, user4, user5 posts, I can retrieve single user posts for example user1 posts or user4 posts. – Rkdio Dec 15 '21 at 10:46
  • So in summary what i'm trying to do is only retrieve the data of specific users. – Rkdio Dec 15 '21 at 10:49
  • thats exactly my comment about You get list of user and then get all property of user in sublist – Tony Hart Dec 15 '21 at 12:58
  • the easy way is to wrap using future builder in list of user then make it expandable widget so if user click its get property of specific user – Tony Hart Dec 15 '21 at 12:59
  • I added StreamBuilder part of my code. If it won't be difficult, can you explain the example you mentioned through the code? – Rkdio Dec 15 '21 at 13:40
  • I solved my problem and I uploaded the answer, if anyone is curious, you can take a look. – Rkdio Dec 21 '21 at 11:18