toMap method

  1. @override
Map<String, dynamic> toMap()
override

Convert this schema to a map (without @context). Subclasses must implement this.

Implementation

@override
Map<String, dynamic> toMap() {
  // Build image list
  final imageList = <String>[];
  if (image != null) imageList.add(image!);
  if (images != null) imageList.addAll(images!);

  return {
    '@type': 'Product',
    'name': name,
    if (description != null) 'description': description,
    if (imageList.length == 1) 'image': imageList.first,
    if (imageList.length > 1) 'image': imageList,
    if (brand != null) 'brand': {'@type': 'Brand', 'name': brand},
    if (sku != null) 'sku': sku,
    if (gtin != null) 'gtin': gtin,
    if (mpn != null) 'mpn': mpn,
    if (url != null) 'url': url,
    if (color != null) 'color': color,
    if (material != null) 'material': material,
    if (offer != null) 'offers': offer!.toMap(),
    if (offers != null && offers!.isNotEmpty)
      'offers': offers!.map((o) => o.toMap()).toList(),
    if (aggregateRating != null) 'aggregateRating': aggregateRating!.toMap(),
    if (reviews != null && reviews!.isNotEmpty)
      'review': reviews!.map((r) => r.toMap()).toList(),
    if (category != null) 'category': category,
  };
}