我正在与JSON序列化作斗争。
我犯了一个错误:“未处理的异常:类型“String”不是类型“num?' in type cast'。
我尝试使用自定义JSON转换器,但它不工作。当我尝试从API获取数据时,它会发生,当它开始执行JSON时。
有谁知道是怎么回事吗?
当它试图解析纬度和纬度值时会发生错误。当它解析这两个值时,它抛出一个异常,即String类型不是num类型的子类型。在类型转换中。在car_model.g.dart文件中,当它运行这两行代码时,会发生这种情况:
lat: (json['lat'] as num?)?.toDouble(),
lng: (json['lng'] as num?)?.toDouble(),
[
{
"_id": "5e5e40c4c0ea272d00000956",
"brand": "Tofas",
"model": "Sahin",
"color": "#0fc0fc",
"registration": "WA12345",
"year": "2005-01-01T00:00:00.000Z",
"ownerId": "5e5e3d7fc0ea272d00000824",
"lat": 50.754,
"lng": 12.2145
},
]
实体
import 'package:equatable/equatable.dart';
abstract class Car extends Equatable {
final String? id;
final String? brand;
final String? model;
final String? color;
final String? registration;
final String? year;
final String? ownerId;
final double? lat;
final double? lng;
const Car({
this.id,
this.brand,
this.model,
this.color,
this.registration,
this.year,
this.ownerId,
this.lat,
this.lng,
});
@override
List<Object?> get props => [
id,
brand,
model,
color,
registration,
year,
ownerId,
lat,
lng,
];
}
型号
part 'car_model.g.dart';
class CarModelList {
final List<CarModel> carModelList;
CarModelList({
required this.carModelList,
});
factory CarModelList.fromJson(List<dynamic> parsedJson) {
List<CarModel> carModelList = <CarModel>[];
carModelList = parsedJson.map((i) => CarModel.fromJson(i)).toList();
return CarModelList(carModelList: carModelList);
}
}
@JsonSerializable()
class CarModel extends Car {
const CarModel({
super.id,
super.brand,
super.model,
super.color,
super.registration,
super.year,
super.ownerId,
super.lat,
super.lng,
});
@JsonKey(name: '_id')
@override
String? get id;
factory CarModel.fromJson(Map<String, dynamic> json) =>
_$CarModelFromJson(json);
Map<String, dynamic> toJson() => _$CarModelToJson(this);
}
模型生成
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'car_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
CarModel _$CarModelFromJson(Map<String, dynamic> json) => CarModel(
id: json['_id'] as String?,
brand: json['brand'] as String?,
model: json['model'] as String?,
color: json['color'] as String?,
registration: json['registration'] as String?,
year: json['year'] as String?,
ownerId: json['ownerId'] as String?,
lat: (json['lat'] as num?)?.toDouble(),
lng: (json['lng'] as num?)?.toDouble(),
);
Map<String, dynamic> _$CarModelToJson(CarModel instance) => <String, dynamic>{
'brand': instance.brand,
'model': instance.model,
'color': instance.color,
'registration': instance.registration,
'year': instance.year,
'ownerId': instance.ownerId,
'lat': instance.lat,
'lng': instance.lng,
'_id': instance.id,
};
3条答案
按热度按时间rbpvctlc1#
这是因为有时你会得到
这意味着它是字符串。所以你可以按照下面的东西
然后再次使用模型生成器工具生成模型。
对于模型的自动创建,你可以使用https://ashamp.github.io/jsonToDartModel/,它也提供了空安全的功能。
kninwzqo2#
]
数组末尾多了一个逗号。我希望这就是问题所在
您可以使用https://app.quicktype.io/网站从json模型到dart模型转换。
TOFANOK <3
lsmepo6l3#
你可以尝试另一个代码生成器,并完全控制你得到什么。
这就是
CarModel
类的描述。生成器.
生成的源代码:
输出量: