StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection('posts')
.doc(firstsnapshot.docs[index]
.data()['postId'])
.snapshots(),
builder: (context,
AsyncSnapshot<
DocumentSnapshot>
snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const SizedBox();
}
if (snapshot.hasError) {
return Center(
child: Text(
'Error: ${snapshot.error}'),
);
}
// Check if the document exists and has the 'likes' field
bool hasLikesField = snapshot
.hasData &&
snapshot.data!.data()
is Map<String,
dynamic> &&
(snapshot.data!.data()
as Map<String,
dynamic>)
.containsKey('likes');
// Get the likes count or default to 0
int likesCount = hasLikesField
? (snapshot.data!.data()
as Map<String,
dynamic>)[
'likes']
.length
: 0;
return CustomText(
size: 16,
text: likesCount.toString(),
textColor: whiteColor,
);
},
)
Comments
Post a Comment