Component.fromJson constructor

Component.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory Component.fromJson(Map<String, dynamic> json) {
  // Parse the `value` field based on the `type`.
  final type = json['type'] as String;
  dynamic value;

  if (type == 'image' || type == 'button' || type == 'video') {
    // For `image`, `button`, and `video`, `value` is a String.
    value = json['value'] as String? ?? '';
  } else {
    // For other types, `value` is a List of `Value`.
    value = (json['value'] as List<dynamic>?)
            ?.map((valueJson) => Value.fromJson(valueJson))
            .toList() ??
        <Value>[];
  }

  return Component(
    direction: json['direction'] as String? ?? '',
    type: type,
    value: value,
    fontFamily: json['fontFamily'] as String?,
    fontSize: json['fontSize'] as int?,
    textAlign: json['textAlign'] as String?,
    align: json['align'] as String?,
    backgroundColor: json['backgroundColor'] as String?,
    borderRadius: json['borderRadius'] as int?,
    color: json['color'] as String?,
    fontWeight: json['fontWeight'] as String?,
    paddingX: json['paddingX'] as int?,
    paddingY: json['paddingY'] as int?,
    url: json['url'] as String?,
  );
}