skip to Main Content

errror Exception: type ‘String’ is not a subtype of type ‘Map<String, dynamic>

When I call the api for input, there is an exception error in the form of String’ is not a subtype of type ‘Map<String, dynamic>. How do I fix this code

enter image description here

This meter_model.dart :

class MeterDataModel {
  int idUnit;
  double startMeter;
  int idUtilities;
  double endMeter;
  String fotoMeter;
  bool flagId;
  DateTime createdDate;
  DateTime periode;
  int createdUser;

  MeterDataModel({
    required this.idUnit,
    required this.startMeter,
    required this.idUtilities,
    required this.endMeter,
    required this.fotoMeter,
    required this.flagId,
    required this.createdDate,
    required this.periode,
    required this.createdUser,
  });

  factory MeterDataModel.fromJson(Map<String, dynamic> json) => MeterDataModel(
        idUnit:int.parse(json["id_unit"]) ,
        startMeter: double.parse(json["start_meter"]),
        idUtilities: json["id_utilities"],
        endMeter: double.parse(json["end_meter"]),
        fotoMeter: json["foto_meter"],
        flagId: json["flag_id"],
        createdDate: DateTime.parse(json["created_date"]),
        periode: DateTime.parse(json["periode"]),
        createdUser: int.parse(json["created_user"]) ,
      );

  Map<String, dynamic> toJson() => {
        "id_unit": idUnit,
        "start_meter": startMeter,
        "id_utilities": idUtilities,
        "end_meter": endMeter,
        "foto_meter": fotoMeter,
        "flag_id": flagId,
        "created_date": createdDate.toIso8601String(),
        "periode":
            "${periode.year.toString().padLeft(4, '0')}-${periode.month.toString().padLeft(2, '0')}-${periode.day.toString().padLeft(2, '0')}",
        "created_user": createdUser,
      };
}

enter image description here

2

Answers


  1. Please try json_decode(...) and please add your whole console error text not screenshot

    Login or Signup to reply.
  2. I saw your Request and Response . Response code is 200. It means API is executed successfully.
    In response , some parameter is coming as in Map but you are parsing as String . Kindly print the response and see the response carefully .

    Parse the String into JSON as:-

     final body = json.decode(ANYSTRING Format of Map);
    

    And Update your Model accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search