Node<T>.fromJson constructor
Creates a node from JSON data.
This factory constructor deserializes a node from a JSON map. The custom
data type T must be deserialized using the provided fromJsonT function.
Parameters:
json- The JSON map containing node datafromJsonT- Function to deserialize the custom data of typeT
Returns a new Node instance with data from the JSON.
Implementation
factory Node.fromJson(
Map<String, dynamic> json,
T Function(Object? json) fromJsonT,
) {
return Node<T>(
id: json['id'] as String,
type: json['type'] as String,
position: json['position'] != null
? const OffsetConverter().fromJson(
json['position'] as Map<String, dynamic>,
)
: Offset.zero,
data: fromJsonT(json['data']),
size: json['size'] != null
? const SizeConverter().fromJson(
json['size'] as Map<String, dynamic>,
)
: null,
// Let the constructor use its default
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,
)
// Set the observable values after construction
..position.value = json['position'] != null
? const OffsetConverter().fromJson(
json['position'] as Map<String, dynamic>,
)
: Offset.zero
..zIndex.value = (json['zIndex'] as num?)?.toInt() ?? 0
..selected.value = (json['selected'] as bool?) ?? false;
}