dart - Dart fromJson 无法解析双精度值
问题描述
我在颤振中使用 fromJson 时遇到问题。我收到这样的数据
{id: d7ba912e-69fd-11ea-9ab0-6597c4120b03, receipt_date: 2020-03-18T17:30:00.000Z, customer_id: e3fedf5e, amount: 2500, remark: Amount 2500, created_on: 2020-03-19T16:22:35.000Z, collectionSize: 3, name: Customer 1}
我正在使用这个 fromJson 来解析:
factory CashReceipt.fromJson(Map<String, dynamic> json) {
return CashReceipt(
id: json['id'],
customerId: json['customer_id'],
date: json['date'] as DateTime,
amount: json['amount'] as double,
remark: json['remark']);
}
“金额”属性在我的模型中是双倍的。它导致以下错误。来自 API 的数据是“金额:2500”。如果我将“数量”更改为“int”,它就会消失。但这是不对的。非常感谢您的帮助。
_CastError (type 'int' is not a subtype of type 'double' in type cast)
解决方案
推荐阅读
- javascript - 将字符串与变量和转义字符连接起来
- json - 选择数组的元素并就地更新它
- jupyter-notebook - Anaconda Navigator Jupyter 和 Spyder 安装成功后无法启动
- c# - 如何将列表值的组合字符串发布到消息框
- android - 与分页库合并时,recyclerview 适配器内的 [android-architecture-components]onClickListener 工作异常
- javascript - JavaScript ReferenceError:按钮未定义
- r - 对聚类树状图的调整
- javascript - 如何使用 Next.js 在每一侧添加组件?
- javascript - 获取一个变量内的数字总和
- node.js - 与 Windows Media Player 集成(当前播放歌曲)