【问题标题】:Querying data from firebase using if else使用 if else 从 firebase 查询数据
【发布时间】:2020-12-04 01:08:11
【问题描述】:

我已使用以下代码从我的 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"];
      });
    }
  }

数据由数据库ID、电子邮件地址和确定用户是否处于活动状态的isActive 方法组成。这 isActive 是数据库中的布尔值,具有 true 和 false 方法。 现在我想写一个查询,如果 isActive 为假,它应该打印“用户未激活”,否则它应该打印用户处于活动状态。我已经写了这段代码,但它给出了错误


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

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

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

【问题讨论】:

  • 你能分享一下你遇到了什么错误
  • 对“this”表达式的引用无效
  • getIsActive 是如何被调用的?看来isActive不在getIsActive的范围内。
  • 你能分享任何简单的方法吗?我实际上正在使用以前可用的代码
  • 你在哪里打电话getIsActive()?这也与节点或javascript无关

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

将类更改为以下内容:

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,因此您应该分配 truefalse 而不是 string。然后要将isActive 分配给一个变量,您必须创建一个User 类的实例:

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

【讨论】:

  • 是的。之后如何根据 if else 查询?
  • if (isActive == false) { return false; } else { return true; }
  • 你得到了什么?
  • 在这一行 bool isActive = user.isActive; 我收到“无法使用静态访问访问实例成员 'isActive'”
  • 能否分享完整的代码。我真的很难弄清楚这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
相关资源
最近更新 更多