【发布时间】:2021-02-02 04:54:09
【问题描述】:
问题是我想从这个解析: {"cost": 0} 加倍。所以我现在拥有的:
- json['cost'].toDouble(),错误:未处理的异常:NoSuchMethodError:类'String'没有实例方法'toDouble'。
- int.parse(json['cost']).toDouble(), error: Unhandled Exception: type 'int' is not a subtype of type 'String'
- json['cost'],错误:未处理的异常:类型'int'不是类型'double'的子类型
完整代码:
factory ShippingMethod.fromJson(Map<String, dynamic> json) {
dynamic cost = json['cost'];
print(cost.runtimeType); // printing runtime type and I get it twice!
return ShippingMethod(
code: json['code'],
title: json['title'],
description: json['description'],
cost: cost.toDouble(),
taxClassId: json['tax_class_id'],
);
}
然后打印:
I/flutter ( 480): int // first it gives me int
I/flutter ( 480): String // second it gives me String
E/flutter ( 480): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance method 'toDouble'.
E/flutter ( 480): Receiver: "0"
E/flutter ( 480): Tried calling: toDouble()
此外,这是我从服务器获得的:
...
"pickup": {
"code": "pickup.pickup",
"title": "Standard",
"description": "If the cost of the order ...",
"cost": 0,
"tax_class_id": 0,
"text": "0.00 TMT"
}
...
Dart 成就了我的一天!.. 是 bug 还是什么?
【问题讨论】: