CollectionMapping.withItemMappers constructor
- Type mapperType
Creates a reference to a collection mapper that will be instantiated from the given type.
Use this constructor when you want to provide your own custom collection mapper implementation that can be instantiated automatically and needs access to item-level serializers/deserializers. The mapper you provide will determine how the collection is serialized to and deserialized from RDF.
The generator will create an instance of mapperType to handle collection mapping.
The type must implement Mapper<C> where C is the collection type (e.g., List<T>)
and must have a constructor that accepts optional itemSerializer and itemDeserializer
parameters: Mapper<C> Function({Serializer<T>? itemSerializer, Deserializer<T>? itemDeserializer})
Use this constructor when: Your collection mapper needs to delegate item serialization to the generated or overridden item mappers (e.g., for complex objects, resources, or custom item mapping).
Use CollectionMapping.mapper() instead when: Your collection mapper handles the entire
collection serialization internally without needing item-level delegation.
Example:
class Book {
// Using a custom collection mapper that needs item serializers for complex objects
@RdfProperty(
SchemaBook.chapters,
collection: CollectionMapping.withItemMappers(OrderedChapterListMapper)
)
final List<Chapter> chapters;
}
// The mapper implementation must be accessible to the generator:
class OrderedChapterListMapper implements UnifiedResourceMapper<List<Chapter>> {
final Serializer<Chapter> itemSerializer;
final Deserializer<Chapter> itemDeserializer;
OrderedChapterListMapper({
Serializer<Chapter>? itemSerializer,
Deserializer<Chapter>? itemDeserializer,
}) : itemSerializer = itemSerializer ?? throw ArgumentError('itemSerializer required'),
itemDeserializer = itemDeserializer ?? throw ArgumentError('itemDeserializer required');
// Implementation details...
}
Implementation
const CollectionMapping.withItemMappers(Type mapperType)
: isAuto = false,
factory = mapperType;