fromJson static method

InlineCanvasConfig? fromJson(
  1. Map<String, dynamic> json, {
  2. CampaignCanvasParser parser = const CampaignCanvasParser(),
})

Returns null when the payload is not a usable inline canvas, so the campaign factory can fall through rather than render a broken card.

Implementation

static InlineCanvasConfig? fromJson(
  Map<String, dynamic> json, {
  CampaignCanvasParser parser = const CampaignCanvasParser(),
}) {
  final slotKey = optString(json, 'slotKey').trim();
  if (slotKey.isEmpty) return null;

  final canvasJson = optMap(json, 'canvas');
  if (canvasJson == null) return null;

  final CampaignCanvas canvas;
  try {
    canvas = parser.parse(canvasJson);
  } on FormatException {
    // A canvas version this build cannot read. Collapsing the slot is the
    // right failure: the app shows its own content instead of a broken card.
    return null;
  }

  final designWidth = _finite(optDouble(json, 'designWidth', 0));
  final layout = optMap(json, 'layout');
  return InlineCanvasConfig(
    slotKey: slotKey,
    // The authored canvas spans the full design frame, so its own width is
    // the correct fallback when an older payload omits `designWidth`.
    designWidth: designWidth > 0 ? designWidth : canvas.width,
    cornerRadius: _finite(optDouble(json, 'cornerRadius', 0)),
    margin: InlineCanvasMargin.fromJson(
      layout == null ? null : optMap(layout, 'margin'),
    ),
    canvas: canvas,
  );
}