项目场景:
Flutter中接口response json数据解析为Model
问题描述:
发现某些字段,例如price价格,无论将字段数据类型定为double还是int,都有可能报错,解析失败。
int? firstCount;
int? orderAmount;
int? amountPaid;
int? orderCount;
int? receiverCount;
double? price;
原因分析:
price字段后台可能返回 int或double,所以数据类型无论选哪一个都不合适。
解决方案:
将这些数值型字段的数据类型定义为num就可以了。
num? firstCount;
num? orderAmount;
num? amountPaid;
num? orderCount;
num? receiverCount;
num? price;