extractMap static method

Map<String, Object?>? extractMap(
  1. String text
)

Extracts a JSON object, or null when the response holds no object.

An array is accepted when it contains a single object, because models sometimes wrap a lone result in a list.

Implementation

static Map<String, Object?>? extractMap(String text) {
  final value = extract(text);
  if (value is Map<String, Object?>) return value;
  if (value is List && value.length == 1 && value.first is Map) {
    return (value.first as Map).cast<String, Object?>();
  }
  return null;
}