readJsonList static method

List<Map<String, dynamic>> readJsonList(
  1. String json
)

Parses a JSON string into a list of Map objects.

May throw a FormatException if the input string is not valid JSON.

Implementation

static List<Map<String, dynamic>> readJsonList(String json) {
  if (json.isEmpty) return [];

  dynamic jsonObj = jsonDecode(json);

  if (jsonObj is List<dynamic>) {
    for (dynamic object in jsonObj) {
      if (object is! Map<String, dynamic>) {
        throw const FormatException(
            "The JSON string is not a list of Map<String, dynamic>");
      }
    }

    return List<Map<String, dynamic>>.from(
      jsonObj.map(
        (result) => Map<String, dynamic>.from(result),
      ),
    );
  }

  return [];
}