【发布时间】:2021-04-30 16:25:30
【问题描述】:
我在 dart 中有一个自定义对象,我将其转换为 json,保存为共享首选项,然后在需要时对其进行解码。
这是课程:
class MaslulModel {
final String name;
final int numberOfMonths;
final int totalAmount;
final int maslulIndex;
final double interest;
MaslulModel(this.name, this.numberOfMonths, this.totalAmount, this.maslulIndex, this.interest);
String getDetails(){
String output = '2124. Maslul Details: Maslul name: ${name}. Maslul Index: ${this.maslulIndex}. Total amount: ${this.totalAmount}. Number of months: ${this.numberOfMonths}. Interest: ${this.interest}';
return output;
}
// To save list as pref
factory MaslulModel.fromJson(Map<String, dynamic> jsonData) {
return MaslulModel(
jsonData['name'],
jsonData['numberOfMonths'],
jsonData['totalAmount'],
jsonData['maslulIndex'],
jsonData['interest;'],
);
}
static Map<String, dynamic> toMap(MaslulModel maslulModel) => {
'name': maslulModel.name,
'numberOfMonths': maslulModel.numberOfMonths,
'totalAmount': maslulModel.totalAmount,
'maslulIndex': maslulModel.maslulIndex,
'interest': maslulModel.interest,
};
static String encode(List<MaslulModel> musics) => json.encode(
musics
.map<Map<String, dynamic>>((music) => MaslulModel.toMap(music))
.toList(),
);
static List<MaslulModel> decode(String musics) =>
(json.decode(musics) as List<dynamic>)
.map<MaslulModel>((item) => MaslulModel.fromJson(item))
.toList();
}
现在,当我使用解码功能时,所有值都被完美保存,兴趣字段是一个double,返回null。
例如,返回的字符串是:
[{"name":"ברמן","numberOfMonths":5,"totalAmount":500,"maslulIndex":0,"interest":5.0}]
这很好,表明利息是 5.0(双倍)。但是当这是解码时,对对象的兴趣是空的。
为什么会这样。
感谢您的帮助
【问题讨论】: