【发布时间】: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 是的空先生