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

This commit is contained in:
2026-01-27 14:03:24 +02:00
parent 6791623b19
commit 0464fa0851
2 changed files with 29 additions and 37 deletions

View File

@@ -70,46 +70,39 @@ 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;
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;
}
// 3. Create the node with the EXPLICIT generic type <T>
final newNode = AstromicFieldNode<T>( final newNode = AstromicFieldNode<T>(
initialValue as T, startValue 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?,
); );
_nodes[id] = newNode; _nodes[id] = newNode;
return newNode; return newNode;
} }
// 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
}
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.');
}
Type _typeOf<T>() => T; Type _typeOf<T>() => T;
bool _isNullable<T>() => null is T; bool _isNullable<T>() => null is T;

View File

@@ -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!;