skip to Main Content

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


  1. you can see this. This solution may help you to solve your problem.

    Login or Signup to reply.
  2. You are trying to parse object as list

    For your provided response the parsed data class will be like this.

    class ResponseData {
      InnerData? data;
      bool? success;
      String? message;
    
      ResponseData({this.data, this.success, this.message});
    
      ResponseData.fromJson(Map<String, dynamic> json) {
        data = json['data'] != null ? InnerData.fromJson(json['data']) : null;
        success = json['success'];
        message = json['message'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        if (this.data != null) {
          data['data'] = this.data!.toJson();
        }
        data['success'] = this.success;
        data['message'] = this.message;
        return data;
      }
    }
    

    and

    class InnerData {
      String? id;
      String? code;
      String? date;
    
      InnerData({this.id, this.code, this.date});
    
      InnerData.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        code = json['code'];
        date = json['date'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = this.id;
        data['code'] = this.code;
        data['date'] = this.date;
        return data;
      }
    }
    

    and For parse

    ResponseData parseData = ResponseData.fromJson(jsonDecode(response.body));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search