1

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mohammed Bekele
  • 727
  • 1
  • 9
  • 25

1 Answers1

0

Somehow your code first loads the data into a `StreamBuilder, but then in there goes back to the database to load the same data again.

Instead of that, get the data from the snapshot with:

StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection("likes")
      .doc(FirebaseAuth
          .instance.currentUser!.email)
      .collection("liked")
      .doc(id)
      .snapshots(),
  builder: (context, asyncSnapshot) {
    bool liked = false;

    if (asyncSnapshot.hasData) {
      var docSnapshot = asyncSnapshot.data!;

      liked = !docSnapshot.exists;
    }
    ...

Since some of your confusion likely comes from the different types of snapshots involved, I recommend also reading my answer to What is the difference between existing types of snapshots in Firebase?.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • do you mean instead of `StreamBuilder`? – Mohammed Bekele Mar 31 '23 at 23:21
  • 1
    Oof, I now see what you're doing. There's no need to create a second reference to the document. All its data is already in the snapshot you get. I added an explanation. --- Also see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Frank van Puffelen Apr 01 '23 at 03:12