asJsonObject function

Map<String, dynamic> asJsonObject(
  1. dynamic body,
  2. String what
)

body as a JSON object, or a FormatException naming what arrived instead.

what describes the thing being parsed and appears in the message, so a failure points at the endpoint rather than at a line number.

Implementation

Map<String, dynamic> asJsonObject(dynamic body, String what) {
  if (body is Map<String, dynamic>) return body;

  throw FormatException('$what expected a JSON object, got ${_describe(body)}');
}