jsonEncode在Flutter中将对象转换为JSON字符串时生成错误。

huangapple go评论40阅读模式
英文:

jsonEncode generating error while converting object to jsonstring in flutter

问题

以下是要翻译的内容:

For explaining what I am facing problem while creating a jsonstring from object list, I have created this basic demo, actually I am trying to create a backup file for saving records but I am getting an error while jsonEncode.

getting following error

Converting object to an encodable object failed: Instance of 'TransactionModel'

  String id;
  bool isexpense;
  DateTime date;
  double amount;

  TransactionModel({
    this.amount = 0.00,
    required this.id,
    this.isexpense = true,
    required this date,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'isexpense': isexpense,
      'date': date,
      'amount': amount,
    };
  }
}

  List<TransactionModel> trans = [
    TransactionModel(
      date: DateTime.now(),
      id: '1',),
  ];

  String result = jsonEncode(trans);//error bcz of jsonEncode

  print(result);
}

英文:

For explaining what I am facing problem while creating a jsonstring from object list,I have created this basic demo,
actually I am trying to create a backup file for saving records but I am getting an error while jsonEncode.

getting following error

Converting object to an encodable object failed: Instance of &#39;TransactionModel&#39;
class TransactionModel {
  String id;
  bool isexpense;
  DateTime date;
  double amount;

  TransactionModel({
    this.amount = 0.00,
    required this.id,
    this.isexpense = true,
    required this.date,
  });

  Map&lt;String, dynamic&gt; toJson() {
    return {
      &#39;id&#39;: id,
      &#39;isexpense&#39;: isexpense,
      &#39;date&#39;: date,
      &#39;amount&#39;: amount,
    };
  }
}
void main() {

  List&lt;TransactionModel&gt; trans = [
    TransactionModel(
      date: DateTime.now(),
        id: &#39;1&#39;,),
  ];

  String result = jsonEncode(trans);//error bcz of jsonEncode

  print(result);
}

答案1

得分: 1

以下是翻译好的部分:

"你不能使用自定义属性(例如DateTime)对一个object进行编码,你需要首先将它转换为map,然后再进行编码,试试这样做:

void main() {
  List<TransactionModel> trans = [
    TransactionModel(
      date: DateTime.now(),
      id: '1',
    ),
  ];

  var listOfMap = trans.map((e) => e.toJson()).toList();

  String result = jsonEncode(listOfMap);

  print(result);
}
```"

<details>
<summary>英文:</summary>

You can&#39;t encode an `object` with custom property like `DateTime`, you need first convert it to `map`, then encode it, try this:

    void main() {
    
      List&lt;TransactionModel&gt; trans = [
        TransactionModel(
          date: DateTime.now(),
            id: &#39;1&#39;,),
      ];
    
      var listOfMap = trans.map((e) =&gt; e.toJson()).toList();
    
      String result = jsonEncode(listOfMap);
    
      print(result);
    }

</details>



huangapple
  • 本文由 发表于 2023年2月9日 01:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定