Body.fromJson constructor

Body.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory Body.fromJson(Map<String, dynamic> json) {
  // Check if there's a fault first
  final fault = json['Fault'] == null ? null : Fault.fromJson(json['Fault']);

  // Find response key (any key that's not 'Fault')
  final responseKeys = json.keys.where((key) => key != 'Fault').toList();

  Map<String, dynamic> responseMap = <String, dynamic>{};

  if (responseKeys.isNotEmpty) {
    final responseType = responseKeys.first;
    final responseValue = json[responseType];

    // Handle case where the response value might be a string or other type
    if (responseValue is Map<String, dynamic>) {
      responseMap = responseValue;
    } else if (responseValue is String) {
      // If it's a string, wrap it in a map
      responseMap = {responseType: responseValue};
    } else if (responseValue != null) {
      // For other types, wrap them in a map
      responseMap = {responseType: responseValue};
    }
  }

  return Body(
    fault: fault,
    response: responseMap.isEmpty ? json : responseMap,
  );
}