未经手的异常:将对象转换为编码对象失败:' Mesure'的实例
您好,当我读取数据时,我有类别的类别,然后将其保存在数据库GetStorage GetX上。它可以很好地工作,但是当我添加此测试时,如果mode = display i'm将在模型上添加更多数据并进行映射。我遇到了这个错误。如果不可用,是否可以跳过“首次亮相”和“鳍”?
未经手的异常:将对象转换为编码对象失败: “ Mesure”的实例 我该如何修复!
class Mesure {
late int id;
String status;
late String mode;
late String equipementNumero;
late String temperature;
late String courant = "";
late String debut;
late String fin = "";
Mesure.init(List<String> values, this.id, this.status) {
mode = values[0];
equipementNumero = values[1];
temperature = values[2];
courant = values[3];
if (mode == "display") {
debut = values[4];
fin = values[5];
}
}
Mesure.fromJson(Map<String, dynamic> json)
: id = json['id'],
status = json['status'],
mode = json['mode'],
temperature = json['temperature'],
equipementNumero = json['equipementNumero'],
courant = json['courant'],
debut = json['debut'],
fin = json['fin'];
Map<String, dynamic> toJson() {
if (mode == "display") {
return {
'id': id,
'status': status,
'mode': mode,
'temperature': temperature,
'equipementNumero': equipementNumero,
'courant': courant,
};
} else {
return {
'id': id,
'status': status,
'mode': mode,
'temperature': temperature,
'equipementNumero': equipementNumero,
'courant': courant,
'debut': debut,
'fin': fin,
};
}
}
}
hello i have the class mesure when i read data i use it ass a model then i save it on my database getstorage getX . it works perfectly , but when i add this test if mode = display i 'm going to add more data on my model and do the mapping . i'm getting this error . is it possible to skip "debut" and "fin" if it does not available ?
Unhandled Exception: Converting object to an encodable object failed:
Instance of 'Mesure'
how can i fix it !!
class Mesure {
late int id;
String status;
late String mode;
late String equipementNumero;
late String temperature;
late String courant = "";
late String debut;
late String fin = "";
Mesure.init(List<String> values, this.id, this.status) {
mode = values[0];
equipementNumero = values[1];
temperature = values[2];
courant = values[3];
if (mode == "display") {
debut = values[4];
fin = values[5];
}
}
Mesure.fromJson(Map<String, dynamic> json)
: id = json['id'],
status = json['status'],
mode = json['mode'],
temperature = json['temperature'],
equipementNumero = json['equipementNumero'],
courant = json['courant'],
debut = json['debut'],
fin = json['fin'];
Map<String, dynamic> toJson() {
if (mode == "display") {
return {
'id': id,
'status': status,
'mode': mode,
'temperature': temperature,
'equipementNumero': equipementNumero,
'courant': courant,
};
} else {
return {
'id': id,
'status': status,
'mode': mode,
'temperature': temperature,
'equipementNumero': equipementNumero,
'courant': courant,
'debut': debut,
'fin': fin,
};
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两件事:
首次亮相
才能将其写入JSON( get_storage < /a>);首次亮相
和fin
的值在json(get_storage)阅读时;因此,整个类
mesure
看起来像这样:另外,构造函数中的代码在
tojson
中看起来倒置。在构造函数中不应该像这样的mode!=“ display”
而不是mode ==“ display”
:There are 2 things:
debut
to make it write to JSON (get_storage);debut
andfin
to something when reading from JSON (get_storage);So, the entire class
Mesure
would look like this:Also, the code in the constructor looks inverted in
toJson
. Shouldn't in the constructor be like thismode != "display"
instead ofmode == "display"
: