listFromJson<T extends SchemaItem> function

List<T>? listFromJson<T extends SchemaItem>(
  1. dynamic json
)

Converts a JSON list to a nullable-list of SchemaItem instances of type T.

This helper is used when deserializing an array of items from JSON. Each item in the array is converted to type T.

Example:

final json = [
  {'title': 'Post 1', '_type': 'blog.post'},
  {'title': 'Post 2', '_type': 'blog.post'},
];
final posts = listFromJson<BlogPost>(json);

Returns null if the input is null. Throws ArgumentError if the input is not a List.

Implementation

List<T>? listFromJson<T extends SchemaItem>(dynamic json) {
  if (json == null) {
    return null;
  }

  if (json is! List) {
    throw ArgumentError.value(json, 'json', 'is not a List');
  }

  return json.map((itemJson) {
    final item = VyuhBinding.instance.content.fromJson<T>(itemJson);
    assert(
        item != null,
        _missingTypeRegistrationMessage<T>(
            VyuhBinding.instance.content.provider.schemaType(itemJson)));

    return item!;
  }).toList(growable: false);
}