How to get post likes to update

Viewed 31

How would I make my likes count stay persistent? So when I press 'like' on a post, it gets incremented, however when I hot restart the like disappears and it also doesn't get incremented in my Database. The liking functionality I am working with :

int _likesCount = 0;
  bool _isLiked = false;

  initPostLikes() async {
    bool isLiked =
        await DatabaseMethods.isLikePost(widget.currentUserId, widget.post);
    if (mounted) {
      setState(() {
        _isLiked = isLiked;
      });
    }
  }

  likePost() {
    if (_isLiked) {
      DatabaseMethods.unlikePost(widget.currentUserId, widget.post);
      setState(() {
        _isLiked = false;
        _likesCount--;
      });
    } else {
      DatabaseMethods.likePost(widget.currentUserId, widget.post);
      setState(() {
        _isLiked = true;
        _likesCount++;
      });
    }
    @override
    void initState() {
      super.initState();
      _likesCount = widget.post.likes;
      initPostLikes();
    }
  }

The onPressed function :

Row(
                children: [
                  IconButton(
                    onPressed: likePost,
                    icon: Icon(
                      _isLiked ? Icons.favorite : Icons.favorite_border,
                      color: _isLiked ? Colors.blue : Colors.black,
                    ),
                  ),
                  Text(_likesCount.toString() + ' Likes'),
                ],
              ),

the Database code :

static void likePost(String currentUserId, Post post) {
    DocumentReference postDocProfile =
        postsRef.doc(post.authorId).collection('userPosts').doc(post.id);
    postDocProfile.get().then((doc) {
      int likes = doc['likes'];
      postDocProfile.update({'likes': likes++});
      print('post liked');
    });

    likesRef.doc(post.id).collection('postLikes').doc(currentUserId).static void likePost(String currentUserId, Post post) {
    DocumentReference postDocProfile =
        postsRef.doc(post.authorId).collection('userPosts').doc(post.id);
    postDocProfile.get().then((doc) {
      int likes = doc['likes'];
      postDocProfile.update({'likes': likes++});
      print('post liked');
    });

    likesRef.doc(post.id).collection('postLikes').doc(currentUserId).set({});
  }

  static void unlikePost(String currentUserId, Post post) {
    DocumentReference postDocProfile =
        postsRef.doc(post.authorId).collection('userPosts').doc(post.id);
    postDocProfile.get().then((doc) {
      int likes = doc['likes'];
      postDocProfile.update({'likes': likes--});
    });

    likesRef
        .doc(post.id)
        .collection('postLikes')
        .doc(currentUserId)
        .get()
        .then((doc) => doc.reference.delete());
  }

  static Future<bool> isLikePost(String currentUserId, Post post) async {
    DocumentSnapshot userDoc = await likesRef
        .doc(post.id)
        .collection('postLikes')
        .doc(currentUserId)
        .get();

    return userDoc.exists;
  }({});
  }

  static void unlikePost(String currentUserId, Post post) {
    DocumentReference postDocProfile =
        postsRef.doc(post.authorId).collection('userPosts').doc(post.id);
    postDocProfile.get().then((doc) {
      int likes = doc['likes'];
      postDocProfile.update({'likes': likes--});
    });

    likesRef
        .doc(post.id)
        .collection('postLikes')
        .doc(currentUserId)
        .get()
        .then((doc) => doc.reference.delete());
  }

  static Future<bool> isLikePost(String currentUserId, Post post) async {
    DocumentSnapshot userDoc = await likesRef
        .doc(post.id)
        .collection('postLikes')
        .doc(currentUserId)
        .get();

    return userDoc.exists;
  }

Another thing worth noting is in the Database code the line likesRef.doc(post.id).collection('postLikes').doc(currentUserId).set({});

when I change "set" to "update", the print statement doesn't get reached

0 Answers
Related