From 229ab02f6d86d1dcd2e8cf95525ae698dac28d2b Mon Sep 17 00:00:00 2001 From: Michael Aziz Date: Tue, 27 Jan 2026 14:19:05 +0200 Subject: [PATCH] [FIX] Node initialization logic for non-dynamic values. --- lib/src/form/src/controller.dart | 43 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 79e8019..2bbe9e8 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -72,29 +72,40 @@ class AstromicFormController { // } // 1. Check if it exists if (_nodes.containsKey(id)) { - final existing = _nodes[id]; - if (existing is AstromicFieldNode) { - return existing; + final existingNode = _nodes[id]!; + + // Check if the existing node is already the correct type + if (existingNode is AstromicFieldNode) { + return existingNode; } - // 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 it's a type mismatch (likely ), we must migrate it. + // We create a new node of the correct type using the existing value. + final replacementNode = AstromicFieldNode( + existingNode.value as T, + formatter: (v) => v?.toString() ?? '', + parser: (v) => (T == String ? v : null) as T?, + ); + + // Transfer validators if they exist + replacementNode.validators = existingNode.validators; + + _nodes[id] = replacementNode; + return replacementNode; } - // 2. Determine the starting value for lazy-init - T? startValue = initialValue; - - if (startValue == null) { + // Fallback for new nodes (keep your existing lazy-init logic here) + T? defaultValue = initialValue; + if (defaultValue == null) { if (T == String) - startValue = '' as T; - else if (T == int) - startValue = 0 as T; - else if (T == bool) startValue = false as T; + defaultValue = '' as T; + else if (T == bool) + defaultValue = false as T; + else if (T == int) defaultValue = 0 as T; } - // 3. Create the node with the EXPLICIT generic type final newNode = AstromicFieldNode( - startValue as T, + defaultValue as T, formatter: (v) => v?.toString() ?? '', parser: (v) => (T == String ? v : null) as T?, );