diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 1b7c9d9..2003f0d 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -38,10 +38,45 @@ class AstromicFormController { /// Retrieves a specific [AstromicFieldNode] by its unique [id], creating it if an [initialValue] is provided. AstromicFieldNode node(String id, {T? initialValue}) { + // if (_nodes.containsKey(id)) { + // return (_nodes[id] as dynamic) as AstromicFieldNode; + // } + + // if (initialValue != null || _isNullable()) { + // final newNode = AstromicFieldNode( + // initialValue as T, + // formatter: (v) => v?.toString() ?? '', + // parser: (v) => (T == String ? v : null) as T?, + // ); + // _nodes[id] = newNode; + // return newNode; + // } + + // T? defaultValue; + // if (T == String || T == _typeOf()) { + // defaultValue = '' as T; + // } else if (T == int || T == _typeOf()) { + // defaultValue = 0 as T; + // } else if (T == bool || T == _typeOf()) { + // defaultValue = false as T; + // } + + // if (defaultValue != null) { + // final newNode = AstromicFieldNode( + // defaultValue, + // formatter: (v) => v.toString(), + // parser: (v) => null, + // ); + // _nodes[id] = newNode; + // return newNode; + // } if (_nodes.containsKey(id)) { - return (_nodes[id] as dynamic) as AstromicFieldNode; + // Use 'as' carefully. Since _nodes stores AstromicFieldNode, + // we cast the node itself, not just the value. + return _nodes[id] as AstromicFieldNode; } + // If we have an initial value, or the type is nullable, create the node with type if (initialValue != null || _isNullable()) { final newNode = AstromicFieldNode( initialValue as T, @@ -52,13 +87,14 @@ class AstromicFormController { return newNode; } + // Standard defaults for primitives T? defaultValue; - if (T == String || T == _typeOf()) { + if (T == String) { defaultValue = '' as T; - } else if (T == int || T == _typeOf()) { + } else if (T == int) { defaultValue = 0 as T; - } else if (T == bool || T == _typeOf()) { - defaultValue = false as T; + } else if (T == bool) { + defaultValue = false as T; // This ensures bools start correctly } if (defaultValue != null) { diff --git a/lib/src/form/src/widgets/value_wrapper.dart b/lib/src/form/src/widgets/value_wrapper.dart index 5c2490d..1790259 100644 --- a/lib/src/form/src/widgets/value_wrapper.dart +++ b/lib/src/form/src/widgets/value_wrapper.dart @@ -38,12 +38,7 @@ class _AstromicValueWrapperState extends State> { try { _node = widget.controller.node(widget.nodeId, initialValue: widget.initialValue); } catch (_) { - if (widget.initialValue != null) { - widget.controller.set(widget.nodeId, widget.initialValue as T); - _node = widget.controller.node(widget.nodeId); - } else { - throw Exception('Node ${widget.nodeId} not found or failed to initialize.'); - } + throw Exception('Node ${widget.nodeId}: failed to initialize.'); } if (widget.validators != null) {