vendredi 20 novembre 2020

How does one make if/else for a review board so that if i like or dislikes, the other options go to 0? (flutter/ dart)

Trying to make a board review where a user can like or dislike a board in flutter/dart. If a user likes/dislikes something and later changes their mind to reverse their selection. I'd like the original selection to go back to 0.

Right now if they change their mind, the previous selection goes to -1.

Class Post {
  String body;
  String author;
  int likes = 0;
  int dislikes = 0;
  bool userLiked = false;
  bool userDisLiked = false;

  Post(this.body, this.author);

  void likePost() {
    this.userLiked = !this.userLiked;
    if (this.userLiked) {
      this.likes += 1;
      this.dislikes = 0;
    } else {
      this.likes -= 1;
      this.dislikes = 0;
    }
  }

  void disLikePost() {
    this.userDisLiked = !this.userDisLiked;
    if (this.userDisLiked) {
      this.dislikes += 1;
      this.likes = 0;
    } else {
      this.dislikes -= 1;
      this.likes = 0;
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire