[FIX] Node initialization logic for non-dynamic values.
This commit is contained in:
@@ -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>) {
|
||||||
// If there's a type mismatch, cast it to bypass strict JS-dev checks
|
return existingNode;
|
||||||
// but try to maintain the reference.
|
|
||||||
return (existing as dynamic) as AstromicFieldNode<T>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Determine the starting value for lazy-init
|
// If it's a type mismatch (likely <dynamic>), we must migrate it.
|
||||||
T? startValue = initialValue;
|
// 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)
|
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?,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user