I want to fetch by id and I get this error.
In the MarsatmasResponseModel, when you bring the data with an id of List type, Marsatmas is returned.I get this error.
Parse Error: type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast - response body: {data: {id: 1, code: adc, date: 2023-04-29T00:00:00}}
My getById method is like this.
Future<MarsatMas?> getbyId({required int id}) async {
MarsatMas? _market;
String query = "${NetworkRoutes.MARSATMASGETBYID.rawValue}?id=$id";
final response =
await manager.send<MarsatmasResponseModel, MarsatmasResponseModel>(
query,
parseModel: MarsatmasResponseModel(),
method: RequestType.GET,
);
if (response != null) {
if (response.data != null && response.data!.data != null) {
_market =
response.data!.data!.first;
return _market;
} else {
return null;
}
} else {
return null;
}
}
This is my MarsatmasResponseModel;
class MarsatmasResponseModel extends INetworkModel<MarsatmasResponseModel> {
final List<MarsatMas>? data;
final bool? success;
final String? message;
MarsatmasResponseModel({this.data, this.success, this.message});
@override
MarsatmasResponseModel fromJson(Map<String, dynamic> json) {
return _$MarsatmasResponseModelFromJson(json);
}
@override
Map<String, dynamic>? toJson() {
return _$MarsatmasResponseModelToJson(this);
}
factory MarsatmasResponseModel.fromJson(Map<String, dynamic> json){
return _$MarsatmasResponseModelFromJson(json);
}
}
ChatGbt suggested me to do it this way=> parseModel: (Map<String, dynamic>? json) => MarsatmasResponseModel.fromJson(json??{}),
When I do it like ChatGbt I get this error
The argument type MarsatmasResponseModel Function(Map<String, dynamic>?) cant be assigned to the parameter type MarsatmasResponseModel.
2
Answers
you can see this. This solution may help you to solve your problem.
You are trying to parse object as list
For your provided response the parsed data class will be like this.
and
and For parse