ApiErrorResponse.fromJson constructor

ApiErrorResponse.fromJson(
  1. Map<String, dynamic> json,
  2. int statusCode
)

Factory constructor to parse JSON data and create an ApiErrorResponse object.

json is the API response body as a Map<String, dynamic>. statusCode is the HTTP status code for the response.

Implementation

ApiErrorResponse.fromJson(Map<String, dynamic> json, int statusCode) {
  String parsedMessage = "";

  // Check if the 'message' field is a Map and handle dynamic error messages.

  if (json['message'] is Map<String, dynamic>) {
    try {
      json['message'].forEach((key, value) {
        parsedMessage += "${List<String>.from(value).first} ";
      });
    } catch (e) {
      parsedMessage = json['message'].toString();
    }
  } else {
    parsedMessage = json['message'] ?? "";
  }

  message = parsedMessage.trim();
  statusCode = statusCode;
  status = json['status'] ?? "";
  data = json['data'] ?? "";
  validation = json['validate'] ?? "";
}