【问题标题】:Dart. Decode json double gives null镖。解码 json double 给出 null
【发布时间】: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(双倍)。但是当这是解码时,对对象的兴趣是空的。

为什么会这样。

感谢您的帮助

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:

    jsonData['interest;'], 正在寻找interest; 的密钥。没想到这么简单吧?

    【讨论】:

    • 这就是为什么代码审查是强制性的,而且绝对应该是你以外的人!
    猜你喜欢
    • 2021-04-05
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2019-05-10
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多