【发布时间】:2020-08-06 18:59:48
【问题描述】:
我有一个非常简单的方法,可以将动态变量转换为 int,如下所示:
int _convertedNum(dynamic number){
int intNumber = int.parse(number);
return intNumber;
}
我有这个模型类:
class Properties {
dynamic popEst;
dynamic confirmed;
Properties({
this.popEst,
this.confirmed
});
factory Properties.fromJson(String str) => Properties.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Properties.fromMap(Map<String, dynamic> json) => Properties(
popEst: json["pop_est"],
confirmed: json["confirmed"]
);
Map<String, dynamic> toMap() => {
"pop_est": popEst,
"confirmed": confirmed
};
}
现在,当我尝试使用该方法将上面类中的属性从动态转换为 int 时:
_convertedNum(properties.popEst);
我得到 type 'int' is not a subtype of type 'String' 错误。我尝试将属性从动态更改为字符串,仍然是同样的错误。
我也尝试对 json 执行此操作:
confirmed: json["confirmed"].toInt();
也抛出类似的错误。
请问如何将属性转换为整数?
【问题讨论】:
-
房产编号是多少?