Flutter报错: type ‘double‘ is not a subtype of type ‘int?‘或type ‘int‘ is not a subtype of type ‘double

项目场景:

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;

猜你喜欢

转载自blog.csdn.net/Jackson_Wen/article/details/119111807
今日推荐