【发布时间】:2018-12-23 00:41:07
【问题描述】:
weight是一个字段(Firestore中的数字),设置为100。
int weight = json['weight'];
double weight = json['weight'];
int weight 工作正常,按预期返回 100,但 double weight 崩溃(Object.noSuchMethod 异常)而不是返回 100.0,这是我的预期。
但是,以下工作:
num weight = json['weight'];
num.toDouble();
【问题讨论】:
-
double weight = json['weight'].toDouble()? -
你可以这样做:
double weight = json['weight'] + 0.0; -
它是 Dart,而不是 Flutter 本身。 Dart 对打字非常严格。最终,它使代码更清晰。但需要一点时间来适应它——Dart 不会为你神奇地转换变量。
标签: dart google-cloud-firestore flutter