【问题标题】:The getter '(' isn't defined for the type没有为类型定义 getter '('
【发布时间】:2021-11-22 07:57:36
【问题描述】:

我有这个代码块,我想用它来从 firebase 数据库中检索数据。但是我对代码有疑问,因为它会带来如下错误消息。我需要帮助来解决这个问题。 错误信息

getter '(' 没有为类型 'UsersData' 定义。 尝试导入定义 '(' 的库,将名称更正为现有 getter 的名称,或定义 get

代码

void _getPhoneNos(BuildContext context) async {
fb.Database db = fb.database();
fb.DatabaseReference<ReferenceJsImpl> dataRef = db.ref('users');
dataRef.onValue.listen((event) {
  fb.DataSnapshot snapshot = event.snapshot;

  if (snapshot == null) {
    print("Result is Empty");
    return;
  }
   List<UsersData>usersInfo= snapshot.val();

     usersInfo.forEach((Item) {
    _singleTextFieldcontroller.text = Item.Item("phone");
  });
});

这是用户数据。

  class UsersData {
  String id;
  String name;
  String type;
  String phone;
  String residential;

  UsersData({
    this.id,
    this.name,
    this.type,
    this.phone,
    this.residential,
  });

  UsersData.fromJson(this.id, Map data) {
    name = data['name'];
    type = data['type'];
    phone = data['phone'];
    residential = data['residential'];
  }

  
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    一个's'不见了:

    UserData({
        this.id,
        this.name,
        this.type,
        this.phone,
        this.residential,
      });
    

    应该是

    UsersData({
        this.id,
        this.name,
        this.type,
        this.phone,
        this.residential,
      });
    

    【讨论】:

    • 我编辑并添加了 ''s'' 但它仍然给出错误信息。
    • 我提出了另一个答案。
    【解决方案2】:

    这一行有错误:

    _singleTextFieldcontroller.text = Item.Item("phone");
    

    Item 是 UsersData 的一个实例。如果你想访问他们的电话变量,你应该这样做:

    _singleTextFieldcontroller.text = Item.phone;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 2020-09-02
      • 2021-07-23
      • 2022-10-09
      • 2021-10-19
      • 2021-08-29
      • 2021-05-21
      • 2021-02-03
      相关资源
      最近更新 更多