diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 2003f0d..79e8019 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -70,44 +70,37 @@ class AstromicFormController { // _nodes[id] = newNode; // return newNode; // } + // 1. Check if it exists if (_nodes.containsKey(id)) { - // Use 'as' carefully. Since _nodes stores AstromicFieldNode, - // we cast the node itself, not just the value. - return _nodes[id] as AstromicFieldNode; + final existing = _nodes[id]; + if (existing is AstromicFieldNode) { + return existing; + } + // If there's a type mismatch, cast it to bypass strict JS-dev checks + // but try to maintain the reference. + return (existing as dynamic) 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, - formatter: (v) => v?.toString() ?? '', - parser: (v) => (T == String ? v : null) as T?, - ); - _nodes[id] = newNode; - return newNode; + // 2. Determine the starting value for lazy-init + T? startValue = initialValue; + + if (startValue == null) { + if (T == String) + startValue = '' as T; + else if (T == int) + startValue = 0 as T; + else if (T == bool) startValue = false as T; } - // Standard defaults for primitives - T? defaultValue; - if (T == String) { - defaultValue = '' as T; - } else if (T == int) { - defaultValue = 0 as T; - } else if (T == bool) { - defaultValue = false as T; // This ensures bools start correctly - } + // 3. Create the node with the EXPLICIT generic type + final newNode = AstromicFieldNode( + startValue as T, + formatter: (v) => v?.toString() ?? '', + parser: (v) => (T == String ? v : null) as T?, + ); - if (defaultValue != null) { - final newNode = AstromicFieldNode( - defaultValue, - formatter: (v) => v.toString(), - parser: (v) => null, - ); - _nodes[id] = newNode; - return newNode; - } - - throw Exception('Node $id not found and cannot be lazy-inited for type $T. Provide an initialValue.'); + _nodes[id] = newNode; + return newNode; } Type _typeOf() => T; diff --git a/lib/src/form/src/widgets/value_wrapper.dart b/lib/src/form/src/widgets/value_wrapper.dart index 1790259..1c27a29 100644 --- a/lib/src/form/src/widgets/value_wrapper.dart +++ b/lib/src/form/src/widgets/value_wrapper.dart @@ -35,11 +35,10 @@ class _AstromicValueWrapperState extends State> { void initState() { super.initState(); - try { - _node = widget.controller.node(widget.nodeId, initialValue: widget.initialValue); - } catch (_) { - throw Exception('Node ${widget.nodeId}: failed to initialize.'); - } + _node = widget.controller.node( + widget.nodeId, + initialValue: widget.initialValue, + ); if (widget.validators != null) { _node.validators = widget.validators!;