parseResponse<T> function

T parseResponse<T>(
  1. Response response,
  2. T parser(
    1. Map<String, dynamic> json
    )
)

Parses a successful Dio response using a custom JSON parser.

Supported response bodies:

  • Map<String, dynamic>: passed directly to parser.
  • Map: accepted only when every key is a String.
  • String: trimmed, JSON-decoded, and parsed as a JSON object.

Non-success status codes and malformed bodies are converted to typed ApiBaseException instances so callers can handle failures consistently.

Implementation

T parseResponse<T>(
  Response<dynamic> response,
  T Function(Map<String, dynamic> json) parser,
) {
  _throwForUnsuccessfulStatus(response);

  final json = _readJsonObject(response.data);

  try {
    return parser(json);
  } on ApiBaseException {
    rethrow;
  } catch (error) {
    throw NetworkException('Failed to parse response into $T: $error');
  }
}