I'm struggling with json serialization.
It throws me na error: "Unhandled Exception: type 'String' is not a subtype of type 'num?' in type cast".
I was trying to use a custom JSON converter, but it doesn't work. It happens when I try to fetch data from API and when it starts do deserialize JSON.
Does anyone have a clue what's wrong?
The error happens when it tries to parse lat and lng values. When it parses these two values, it throws an exception that type String ist not a subtype of type num? in type cast. It happens in car_model.g.dart file when it runs these two lines:
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
},
]
Entity
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,
];
}
Model
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);
}
Model generated
// 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,
};
CodePudding user response:
[
{
"_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
}
]
Array has an extra comma at the end. I hope this is the problem
You can use https://app.quicktype.io/ website for from json model to dart model convert.
TOFAŞK <3
CodePudding user response:
It is because sometimes you are getting
"lat": "50.754",
"lng": "12.2145"
it means it is string. So you can follow below thing
final dynamic lat;
final dynamic lng;
then again generate model using your model generator tool.
And for auto creation of model you can use https://ashamp.github.io/jsonToDartModel/ it provide functionality for null safety too.