7

【踩坑】Dart 将 JSON 转换为对象时报错

 1 year ago
source link: https://loli.fj.cn/2023/08/10/Dart%E5%B0%86JSON%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%AF%B9%E8%B1%A1%E6%97%B6%E6%8A%A5%E9%94%99/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【踩坑】Dart 将 JSON 转换为对象时报错

2023-08-10

有一个 JSON 数据,其中某个字段的值会传递整型和浮点型两种类型的值(实际上都是浮点型,只不过浮点型数据以.0 结尾会被省略,导致看起来是一个整型数据)
此时如果为这个字段定义为 int 类型,那么会报错:Unhandled Exception: type 'double' is not a subtype of type 'int'
此时如果为这个字段定义为 double 类型,那么会报错:Unhandled Exception: type 'int' is not a subtype of type 'double'

  • 将这个字段定义为 num 类型即可

Stackoom——Nagual


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK