skip to Main Content

There is a user collection, there is a uid attribute and a uid list follower list. There is a post collection. There is an id unique and uid attribute that equalt to user uid who created post.How can I do that the user I want to see only the posts of the person they follow?

2

Answers


  1. You can use the ‘in’ operator to retrieve posts whose creator is included in the list of followed users.

    final firestore = FirebaseFirestore.instance;
    
    try {
      // Get the specified user document from Firestore
      final userSnapshot = await firestore
          .collection('userCollection')
          .doc('userId')
          .get();
    
      // Check if the user document exists
      if (userSnapshot.exists) {
        // Get the list of followedUsers from the user data
        final followedUsers = List<String>.from(userSnapshot.data()['followedUsers']);
    
        // Query Firestore for every Post document that was created by any followedUsers
        final posts = await firestore
            .collection('postsCollection')
            .where('creator', whereIn: followedUsers)
            .get();
    
        
      } else {
        print('User not found.');
      }
    } catch (e) {
      print('Error retrieving posts: $e');
    }
    
    Login or Signup to reply.
  2. you can store the uids of the persons he follows in his doc and then show the posts accordingly to these uids.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search