【问题标题】:Unhandled Exception: NoSuchMethodError: The getter 'id' was called on null. in Flutter未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'id'。在颤振中
【发布时间】:2020-08-10 03:47:58
【问题描述】:

我试图在每次用户插入或更新数据时显示所有插入的数据,但是我在开发过程中遇到此错误 'Unhandled Exception: NoSuchMethodError: The getter 'id' was called on null.' 任何建议将不胜感激。

这是我的代码

    saveContactData() {
    final database = $FloorContactDatabase.databaseBuilder('Contacts.db').build();
    database.then((onValue) {
      onValue.contactsDao.getMaxTodo().then((isvalue) {
        int id = 1;
        if(onValue != null) {
          print(onValue);
          id = isvalue.id + 1; //MY ERROR IS HERE
        }
          onValue.contactsDao.insertContact(ContactObject(id: id,
              firstname: firstnameController.text,
              lastname: lastnameController.text,
              birthday: int.parse(firstnameController.text.trim()),
              contactnumber: int.parse(contactnumberController.text.trim()),
              profilepicture: Image.file(_image).toString()
          ));


      });
    });
  }

这是我的 repo,如果我调用 id 的话

    @entity
class ContactObject {
    @PrimaryKey(autoGenerate: true)
    int id;
    String firstname;
    String lastname;
    int birthday;
    int contactnumber;
    String profilepicture;

    @ignore
    bool isSelect = false;
    ContactObject ({this.id , this.firstname, this.lastname, this.birthday, this.contactnumber, this.profilepicture});//need to change if needed

    @override
    String toString(){
      return 'ContactObject{id: $id, firstname: $firstname, lastname: $lastname, birthday: $birthday, contactnumber: $contactnumber, profilepicture: $profilepicture}';
    }

    static ContactObject formMap(Map<String, dynamic> extend) {
        if(extend == null) return null;
          ContactObject contacts = ContactObject();
          contacts.id = extend['id'];
          contacts.firstname = extend['firstname'];
          contacts.lastname = extend['lastname'];
          contacts.birthday = extend['birthday'];
          contacts.contactnumber = extend['contactnumber'];
          contacts.profilepicture = extend['profilepicture'];
          return contacts;
    }

  Map<String, dynamic> toMap(){
      return{
        "id": id,
        "firstname": firstname,
        "lastname": lastname,
        "birthday": birthday,
        "contactnumber": contactnumber,
        "profilepicture" : profilepicture
      };
  }

}

这是我在数据库中的查询

`@override
  Future<ContactObject> getMaxTodo() {
    return _queryAdapter.query('SELECT * FROM ContactObject order by id desc limit 1',
        mapper: _todoMapper);
  }
`

【问题讨论】:

  • isvalue 显示了什么?
  • 嗨 @JohnJoe 不先生,我的错误是 isvalue 之后的 id。
  • 尝试打印出 isValue 并查看。也许它是空的?
  • @ShanMichaelReyes 只需调试您的代码即可轻松解决您的问题 :)
  • @JohnJoe 是的空先生

标签: flutter dart floor


【解决方案1】:

只需使用 dart null 避免运算符

if(onValue != null) {
    print(onValue);
    id = isvalue?.id??0 + 1; 
}

在上面的代码中,isvaluenull 然后id 最初回退到0,最后导致1

【讨论】:

  • 嗨,约翰,错误消失了,但 Listview 中没有显示数据 感谢您的回复
猜你喜欢
  • 2022-11-17
  • 1970-01-01
  • 2022-01-16
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
相关资源
最近更新 更多