structuredDataJson method

String? structuredDataJson()

The JSON-LD document for this page, or null when there is nothing to say. Explicit structuredData wins (with @context filled in when omitted); otherwise a schema.org WebPage derives from the properties the page already declares — the spec's "structured data and content schema" without per-page authoring.

Implementation

String? structuredDataJson() {
  final explicit = structuredData;
  if (explicit != null) {
    return jsonEncode(<String, Object?>{
      '@context': 'https://schema.org',
      ...explicit,
    });
  }
  if (title == null && description == null && canonicalUrl == null) {
    return null;
  }
  return jsonEncode(<String, Object?>{
    '@context': 'https://schema.org',
    '@type': 'WebPage',
    if (title != null) 'name': title,
    if (description != null) 'description': description,
    if (canonicalUrl != null) 'url': canonicalUrl,
    if (imageUrl != null) 'image': imageUrl,
    if (siteName != null)
      'isPartOf': <String, Object?>{
        '@type': 'WebSite',
        'name': siteName,
      },
  });
}