[FIX] Node initialization logic for non-dynamic values.

This commit is contained in:
2026-01-27 14:19:05 +02:00
parent 0464fa0851
commit 229ab02f6d

View File

@@ -72,29 +72,40 @@ class AstromicFormController {
// } // }
// 1. Check if it exists // 1. Check if it exists
if (_nodes.containsKey(id)) { if (_nodes.containsKey(id)) {
final existing = _nodes[id]; final existingNode = _nodes[id]!;
if (existing is AstromicFieldNode<T>) {
return existing; // Check if the existing node is already the correct type
if (existingNode is AstromicFieldNode<T>) {
return existingNode;
} }
// If there's a type mismatch, cast it to bypass strict JS-dev checks
// but try to maintain the reference. // If it's a type mismatch (likely <dynamic>), we must migrate it.
return (existing as dynamic) as AstromicFieldNode<T>; // We create a new node of the correct type using the existing value.
final replacementNode = AstromicFieldNode<T>(
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 // Fallback for new nodes (keep your existing lazy-init logic here)
T? startValue = initialValue; T? defaultValue = initialValue;
if (defaultValue == null) {
if (startValue == null) {
if (T == String) if (T == String)
startValue = '' as T; defaultValue = '' as T;
else if (T == int) else if (T == bool)
startValue = 0 as T; defaultValue = false as T;
else if (T == bool) startValue = false as T; else if (T == int) defaultValue = 0 as T;
} }
// 3. Create the node with the EXPLICIT generic type <T>
final newNode = AstromicFieldNode<T>( final newNode = AstromicFieldNode<T>(
startValue as T, defaultValue as T,
formatter: (v) => v?.toString() ?? '', formatter: (v) => v?.toString() ?? '',
parser: (v) => (T == String ? v : null) as T?, parser: (v) => (T == String ? v : null) as T?,
); );