ali262883
ali262883

Reputation: 369

Querying data from firebase using if else

I have retrieved data from my firestore storage using this code:

class User {
  bool isActive;
  String databaseID;
  String email;
  
  User() {
    this.isActive = '';
    this.databaseID = '';
    this.email = '';
  }
  toJson() {
    return {
      'isActive': this.isActive,
      "databaseID": this.databaseID,
      "email": this.email,
      "
    };
  }

  fromJson(Map<String, dynamic> json) {
    this.isActive = json['isActive'];
    this.databaseID = json["databaseID"];
    this.email = json["email"];
      });
    }
  }

The data consists of databaseID, email address and isActive method which determines if a user is active or not. This isActive is a boolean value in the database with true and false methods. Now I want to write a query where if isActive is false it should print "User is NOT active" else it should print User is active. I have written this code but it is giving error


    try {
    // get isActive if available
      bool isActive = this.isActive;

      if (isActive == false) {
        return false;
      }

      else {
        return true;
      }
    } catch (error) {
      print(error);
      return null;
    }
  }

Upvotes: 1

Views: 211

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

Change the class to the following:

class User {
  bool isActive;
  String databaseID;
  String email;
  
  User() {
    this.isActive = true;
    this.databaseID = '';
    this.email = '';
  }
  toJson() {
    return {
      'isActive': this.isActive,
      "databaseID": this.databaseID,
      "email": this.email,
    };
  }

  fromJson(Map<String, dynamic> json) {
    this.isActive = json['isActive'];
    this.databaseID = json["databaseID"];
    this.email = json["email"];
      }
    }

isActive is of type bool so you should either assign true or false not string. Then to assign isActive to a variable you have to create an instance of class User:

User user = new User();
user.isActive = false;
bool isActive = user.isActive;

Upvotes: 1

Related Questions