On the explore page, I get()
the entire users
collection to create a user list and search results. Inside each of those user documents is a collection posts
that I also need to get to create a GridView
of each post. I want to reuse that users
collection QuerySnapshot instead of fetching each posts
collection again to save money. Is this possible?
Here is my current function:
void fetchUsers() async {
final userRef = FirebaseFirestore.instance.collection('users');
final QuerySnapshot result = await userRef.get();
final docs = result.docs.asMap();
docs.forEach((index, value) {
final profile =
ProfileObject.fromJson(value.data() as Map<String, dynamic>);
usersList.add(UserSearchResult(profile, value.id));
/// Below is the code for getting the posts, not working, need ideas
final QuerySnapshot postsResult = value.get('posts');
final posts = postsResult.docs.asMap();
posts.forEach((index, value) {
final post = Post.fromJson(value.data() as Map<String, dynamic>);
postsList.add(post);
});
});
print(usersList);
print(postsList);
}
Here is the structure of my Firestore:
- users
- uid (doc)
- posts (collection)
- info (fields)
- uid (doc)
- posts (collection)
- info (fields)
- uid (doc)