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