extractListOfJsonObjects method
Extracts a List of JSON objects (Map<String, dynamic>from dynamic value. Validates both the list structure and that all elements are properly formatted. Throws FormatException for invalid types.
Implementation
@override
List<Map<String, dynamic>> extractListOfJsonObjects(
dynamic value,
String errMsg,
) {
if (value is! List) {
throw FormatException(
"[JsonConverter]: expected list, got ${value.runtimeType}: $errMsg",
);
}
final List<Map<String, dynamic>> resultList = [];
for (final element in value) {
if (element is! Map) {
throw FormatException(
"[JsonConverter] expected each element in list to be a map, got ${element.runtimeType}: $errMsg",
);
}
final Map<String, dynamic> typedMap = {};
for (final entry in (element).entries) {
if (entry.key is! String) {
throw FormatException(
"[JsonConverter] expected each key in map to be String, got ${entry.key.runtimeType}: $errMsg",
);
}
typedMap[entry.key as String] = entry.value;
}
resultList.add(typedMap);
}
return resultList;
}