UidlNode.fromJson constructor

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

Implementation

factory UidlNode.fromJson(Map<String, dynamic> json) {
  final rawChildren = json['children'];
  final List<UidlNode> parsedChildren = [];
  if (rawChildren is List) {
    for (final child in rawChildren) {
      if (child is Map<String, dynamic>) {
        parsedChildren.add(UidlNode.fromJson(child));
      }
    }
  }

  Map<String, List<UidlNode>>? parsedSlots;
  final rawSlots = json['slots'];
  if (rawSlots is Map<String, dynamic>) {
    parsedSlots = {};
    for (final entry in rawSlots.entries) {
      if (entry.value is List) {
        parsedSlots[entry.key] = (entry.value as List)
            .whereType<Map<String, dynamic>>()
            .map(UidlNode.fromJson)
            .toList();
      }
    }
  }

  final id = json['id'] as String?;
  final type = json['type'] as String?;
  final componentId = json['componentId'] as String?;

  if (id == null || (type == null && componentId == null)) {
    throw UidlException(
      code: UidlErrorCodes.invalidDocument,
      message: 'A UIDL node must have an "id" and either "type" or "componentId"',
    );
  }

  return UidlNode(
    id: id,
    type: type ?? 'Container',
    componentId: componentId,
    props: json['props'] is Map<String, dynamic>
        ? Map<String, dynamic>.from(json['props'] as Map)
        : const {},
    style: json['style'] is Map<String, dynamic>
        ? Map<String, dynamic>.from(json['style'] as Map)
        : null,
    events: json['events'] is Map<String, dynamic>
        ? Map<String, dynamic>.from(json['events'] as Map)
        : null,
    children: parsedChildren,
    slots: parsedSlots,
    visibility: json['visibility'] is Map<String, dynamic>
        ? UidlVisibility.fromJson(json['visibility'] as Map<String, dynamic>)
        : null,
    repeat: json['repeat'] is Map<String, dynamic>
        ? UidlRepeat.fromJson(json['repeat'] as Map<String, dynamic>)
        : null,
    themeRef: json['themeRef'] as String?,
    testId: json['testId'] as String?,
  );
}