【发布时间】:2019-02-02 06:49:17
【问题描述】:
我必须为我的自定义对象制作 json 数据,下面的代码显示了我的模型。
import 'package:meta/meta.dart';
class KakaoMLModel {
final List<List<double>> jaw;
final List<List<double>> rightEyebrow;
final List<List<double>> leftEyebrow;
final List<List<double>> nose;
final List<List<double>> rightEye;
final List<List<double>> leftEye;
final List<List<double>> lip;
KakaoMLModel({
@required this.jaw,
@required this.rightEyebrow,
@required this.leftEyebrow,
@required this.nose,
@required this.rightEye,
@required this.leftEye,
@required this.lip
});
factory KakaoMLModel.fromJson(Map<String,dynamic> json) {
print(json['jaw']);
return KakaoMLModel(
jaw: json['jaw'],
rightEyebrow: json['rightEyebrow'],
leftEyebrow: json['leftEyebrow'],
nose: json['nose'],
rightEye: json['rightEye'],
leftEye: json['leftEye'],
lip: json['lip']
);
}
}
但是,当我通过参数传递json数据时,会出现这样的错误消息。
_TypeError (type 'List<dynamic>' is not a subtype of type 'List<List<double>>'
json['jaw'] 格式如下
[[0.04616761798553884, 0.5475042080412023], [0.026029605771987013, 0.6203330820534684], [0.014870009633410218, 0.6947797773981148], [0.01478418759402894, 0.7684331582472369], [0.03166518933198375, 0.8392744747251855], [0.06115956082472804, 0.9024180638379186], [0.10860571254829726, 0.9601635183836916], [0.17011504626717094, 1.0057204991427136], [0.25121165844788507, 1.0340586363828044], [0.3433729801681099, 1.0413332887822182], [0.4340747633053713, 1.0248020469426704], [0.5180659200853382, 0.9941894832935652], [0.5905636066620379, 0.949568636289012], [0.6483867917457562, 0.8895699510264967], [0.6935845784738898, 0.8206748353165457], [0.7288984252469625, 0.7487434227072808], [0.7554424386870936, 0.6755670567849011]]
我该如何处理这个铸造问题?
【问题讨论】:
-
试试
json['jaw'] as List<List<double>> -
或将下巴设为
final List jaw而不是具体的。 -
您的建议无效
-
json['jaw'].cast
- >() 应该可以解决问题