如何解决使用if else从firebase查询数据
我已使用以下代码从Firestore存储中检索了数据:
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"];
});
}
}
数据由databaseID,电子邮件地址和isActive方法组成,该方法确定用户是否处于活动状态。 isActive是数据库中具有true和false方法的布尔值。 现在,我想编写一个查询,如果isActive为false,则应打印“ User is not active”,否则应打印“ User is active”。我已经编写了这段代码,但是它给出了错误
try {
// get isActive if available
bool isActive = this.isActive;
if (isActive == false) {
return false;
}
else {
return true;
}
} catch (error) {
print(error);
return null;
}
}
解决方法
将类更改为以下内容:
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
的类型为bool
,因此您应该分配true
或false
而不是string
。然后要将isActive
分配给变量,您必须创建类User
的实例:
User user = new User();
user.isActive = false;
bool isActive = user.isActive;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。