Node<T>.fromJson constructor

Node<T>.fromJson(
  1. Map<String, dynamic> json,
  2. T fromJsonT(
    1. Object? json
    )
)

Creates a node from JSON data.

Implementation

factory Node.fromJson(
  Map<String, dynamic> json,
  T Function(Object? json) fromJsonT,
) {
  // Parse layer from string if present
  final layerStr = json['layer'] as String?;
  final layer = layerStr != null
      ? NodeRenderLayer.values.firstWhere(
          (l) => l.name == layerStr,
          orElse: () => NodeRenderLayer.middle,
        )
      : NodeRenderLayer.middle;

  // Parse position from x, y at top level
  final position = Offset(
    (json['x'] as num?)?.toDouble() ?? 0,
    (json['y'] as num?)?.toDouble() ?? 0,
  );

  // Parse size from width, height at top level
  final size = json.containsKey('width') && json.containsKey('height')
      ? Size(
          (json['width'] as num).toDouble(),
          (json['height'] as num).toDouble(),
        )
      : null;

  return Node<T>(
      id: json['id'] as String,
      type: json['type'] as String,
      position: position,
      data: fromJsonT(json['data']),
      size: size,
      inputPorts:
          (json['inputPorts'] as List<dynamic>?)
              ?.map((e) => Port.fromJson(Map<String, dynamic>.from(e as Map)))
              .toList() ??
          const [],
      outputPorts:
          (json['outputPorts'] as List<dynamic>?)
              ?.map((e) => Port.fromJson(Map<String, dynamic>.from(e as Map)))
              .toList() ??
          const [],
      initialZIndex: (json['zIndex'] as num?)?.toInt() ?? 0,
      visible: (json['isVisible'] as bool?) ?? true,
      layer: layer,
      locked: (json['locked'] as bool?) ?? false,
      selectable: (json['selectable'] as bool?) ?? true,
    )
    // Set the observable values after construction
    ..position.value = position
    ..zIndex.value = (json['zIndex'] as num?)?.toInt() ?? 0
    ..selected.value = (json['selected'] as bool?) ?? false;
}