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],
),
);
}
}
},
),