[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
if (_nodes.containsKey(id)) {
final existing = _nodes[id];
if (existing is AstromicFieldNode<T>) {
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<T>;
final existingNode = _nodes[id]!;
// Check if the existing node is already the correct type
if (existingNode is AstromicFieldNode<T>) {
return existingNode;
}
// 2. Determine the starting value for lazy-init
T? startValue = initialValue;
// If it's a type mismatch (likely <dynamic>), we must migrate it.
// 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?,
);
if (startValue == null) {
// Transfer validators if they exist
replacementNode.validators = existingNode.validators;
_nodes[id] = replacementNode;
return replacementNode;
}
// 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 <T>
final newNode = AstromicFieldNode<T>(
startValue as T,
defaultValue as T,
formatter: (v) => v?.toString() ?? '',
parser: (v) => (T == String ? v : null) as T?,
);