diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index bb9fc7a..149248c 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -106,17 +106,6 @@ class AstromicFormController { return node(id).value; } - /// Safely attempts to retrieve a value, returning `null` if the node is missing or of a different type. - // T? tryGet(String id) { - // if (_nodes.containsKey(id)) { - // final n = _nodes[id]; - // if (n is AstromicFieldNode) { - // return n.value; - // } - // } - // return null; - // } - T? tryGet(String id) { final n = _nodes[id]; if (n == null) return null; @@ -128,10 +117,10 @@ class AstromicFormController { if (value is T) { return value; } - + // Fallback for null-safety: if T is nullable and value is null if (value == null && _isNullable()) { - return null; + return null; } return null; @@ -276,6 +265,38 @@ class AstromicFormController { }).toList(); } + /// Hydrates a dynamic group with a list of data maps. + /// + /// Each map in [data] represents one instance of the group. + /// This method generates a UUID for each map and populates the fields. + void hydrateGroup(String groupId, List> data, {String? parentUuid}) { + // 1. Clear existing manifest for this group/parent context to prevent duplication + final manifest = getManifest(groupId, parentUuid); + manifest.value = []; + + // 2. Iterate through each data object (row) + for (final rowData in data) { + // 3. Add a fresh instance (this generates the UUID and registers field nodes) + final String newUuid = addGroupInstance( + groupId, + parentUuid: parentUuid, + initialData: rowData, + ); + + // 4. Handle Nested Subgroups (Recursion) + final def = _groupDefs[groupId]; + if (def != null && def.subGroups.isNotEmpty) { + for (final subId in def.subGroups) { + if (rowData.containsKey(subId) && rowData[subId] is List) { + final List> subData = List>.from(rowData[subId]); + + hydrateGroup(subId, subData, parentUuid: newUuid); + } + } + } + } + } + /// Retrieves the schema definition for a registered group. AstromicFormGroupDefinition? getGroupDef(String id) => _groupDefs[id];