I've been fetching a post and in my Forestore i have a collection with the name likes
and in this collection i have stored docs with all users email. then i've created a nested collection 'liked' and in this i have a doc with post id's in which that user liked.
so to get these i've created a StreamBuilder
:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("likes")
.doc(FirebaseAuth
.instance.currentUser!.email)
.collection("liked")
.doc(id)
.snapshots(),
builder: (context, snapshot) {
bool liked = false;
if (snapshot.hasData) {
var col = FirebaseFirestore.instance
.collection("likes");
var doc = col.doc(FirebaseAuth
.instance.currentUser!.email);
var col2 = doc.collection("liked");
var doc2 = col2.doc(id).get();
if (doc2 == null) {
liked = false;
} else {
liked = true;
}
}
return Row(
children: [
// Text(snapshot.data.toString()),
IconButton(
onPressed: () {
liked
? FirebaseFirestore.instance
.collection("likes")
.doc(FirebaseAuth.instance
.currentUser!.email)
.delete()
: FirebaseFirestore.instance
.collection("likes")
.doc(FirebaseAuth.instance
.currentUser!.email)
.collection("liked")
.add({"like": true});
},
icon: GradientIcon(
height: height,
width: width,
icon: liked
? CupertinoIcons.heart_fill
: CupertinoIcons.heart,
gradient: const LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: [
Colors.deepPurple,
Colors.deepPurpleAccent
]),
)),
],
);
}),
in the above code i was trying to check if the user liked that specific post and change the ui accordingly. but i think the keyWord exist
has been deprecated so whenever i try to check if the doc exists it always returns true b/c the snapshot always has data.
so how can i check if the doc exists by using the doc id?