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);

        if (item != null) {
          return item;
        }

        // Create appropriate unknown placeholder based on type
        final schemaType =
            VyuhBinding.instance.content.provider.schemaType(itemJson);

        // For ContentItem, we already have UnknownContentItem
        if (T == ContentItem) {
          return UnknownContentItem(
            missingSchemaType: schemaType,
            jsonPayload: itemJson,
          ) as T;
        }

        // For other types, use the factory method approach
        final unknownPlaceholder =
            _unknownPlaceholderFactory?.call(T, schemaType, itemJson);

        if (unknownPlaceholder != null) {
          return unknownPlaceholder as T;
        }

        // In debug mode, provide helpful error message
        if (kDebugMode) {
          print(_missingTypeRegistrationMessage<T>(schemaType));
        }

        // If no placeholder could be created, we have to skip this item
        return null;
      })
      .where((item) => item != null)
      .cast<T>()
      .toList(growable: false);
}