[FIX] Node initialization logic for non-dynamic values.
This commit is contained in:
@@ -70,44 +70,37 @@ class AstromicFormController {
|
|||||||
// _nodes[id] = newNode;
|
// _nodes[id] = newNode;
|
||||||
// return newNode;
|
// return newNode;
|
||||||
// }
|
// }
|
||||||
|
// 1. Check if it exists
|
||||||
if (_nodes.containsKey(id)) {
|
if (_nodes.containsKey(id)) {
|
||||||
// Use 'as' carefully. Since _nodes stores AstromicFieldNode<dynamic>,
|
final existing = _nodes[id];
|
||||||
// we cast the node itself, not just the value.
|
if (existing is AstromicFieldNode<T>) {
|
||||||
return _nodes[id] as 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>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have an initial value, or the type is nullable, create the node with type <T>
|
// 2. Determine the starting value for lazy-init
|
||||||
if (initialValue != null || _isNullable<T>()) {
|
T? startValue = initialValue;
|
||||||
final newNode = AstromicFieldNode<T>(
|
|
||||||
initialValue as T,
|
if (startValue == null) {
|
||||||
formatter: (v) => v?.toString() ?? '',
|
if (T == String)
|
||||||
parser: (v) => (T == String ? v : null) as T?,
|
startValue = '' as T;
|
||||||
);
|
else if (T == int)
|
||||||
_nodes[id] = newNode;
|
startValue = 0 as T;
|
||||||
return newNode;
|
else if (T == bool) startValue = false as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standard defaults for primitives
|
// 3. Create the node with the EXPLICIT generic type <T>
|
||||||
T? defaultValue;
|
final newNode = AstromicFieldNode<T>(
|
||||||
if (T == String) {
|
startValue as T,
|
||||||
defaultValue = '' as T;
|
formatter: (v) => v?.toString() ?? '',
|
||||||
} else if (T == int) {
|
parser: (v) => (T == String ? v : null) as T?,
|
||||||
defaultValue = 0 as T;
|
);
|
||||||
} else if (T == bool) {
|
|
||||||
defaultValue = false as T; // This ensures bools start correctly
|
|
||||||
}
|
|
||||||
|
|
||||||
if (defaultValue != null) {
|
_nodes[id] = newNode;
|
||||||
final newNode = AstromicFieldNode<T>(
|
return newNode;
|
||||||
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.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Type _typeOf<T>() => T;
|
Type _typeOf<T>() => T;
|
||||||
|
|||||||
@@ -35,11 +35,10 @@ class _AstromicValueWrapperState<T> extends State<AstromicValueWrapper<T>> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
try {
|
_node = widget.controller.node<T>(
|
||||||
_node = widget.controller.node<T>(widget.nodeId, initialValue: widget.initialValue);
|
widget.nodeId,
|
||||||
} catch (_) {
|
initialValue: widget.initialValue,
|
||||||
throw Exception('Node ${widget.nodeId}: failed to initialize.');
|
);
|
||||||
}
|
|
||||||
|
|
||||||
if (widget.validators != null) {
|
if (widget.validators != null) {
|
||||||
_node.validators = widget.validators!;
|
_node.validators = widget.validators!;
|
||||||
|
|||||||
Reference in New Issue
Block a user