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

This commit is contained in:
2026-01-27 13:56:50 +02:00
parent 25b9366131
commit 6791623b19
2 changed files with 42 additions and 11 deletions

View File

@@ -38,10 +38,45 @@ class AstromicFormController {
/// Retrieves a specific [AstromicFieldNode] by its unique [id], creating it if an [initialValue] is provided. /// Retrieves a specific [AstromicFieldNode] by its unique [id], creating it if an [initialValue] is provided.
AstromicFieldNode<T> node<T>(String id, {T? initialValue}) { AstromicFieldNode<T> node<T>(String id, {T? initialValue}) {
// if (_nodes.containsKey(id)) {
// return (_nodes[id] as dynamic) as AstromicFieldNode<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;
// }
// T? defaultValue;
// if (T == String || T == _typeOf<String?>()) {
// defaultValue = '' as T;
// } else if (T == int || T == _typeOf<int?>()) {
// defaultValue = 0 as T;
// } else if (T == bool || T == _typeOf<bool?>()) {
// defaultValue = false as T;
// }
// if (defaultValue != null) {
// final newNode = AstromicFieldNode<T>(
// defaultValue,
// formatter: (v) => v.toString(),
// parser: (v) => null,
// );
// _nodes[id] = newNode;
// return newNode;
// }
if (_nodes.containsKey(id)) { if (_nodes.containsKey(id)) {
return (_nodes[id] as dynamic) as AstromicFieldNode<T>; // Use 'as' carefully. Since _nodes stores AstromicFieldNode<dynamic>,
// we cast the node itself, not just the value.
return _nodes[id] 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>()) { if (initialValue != null || _isNullable<T>()) {
final newNode = AstromicFieldNode<T>( final newNode = AstromicFieldNode<T>(
initialValue as T, initialValue as T,
@@ -52,13 +87,14 @@ class AstromicFormController {
return newNode; return newNode;
} }
// Standard defaults for primitives
T? defaultValue; T? defaultValue;
if (T == String || T == _typeOf<String?>()) { if (T == String) {
defaultValue = '' as T; defaultValue = '' as T;
} else if (T == int || T == _typeOf<int?>()) { } else if (T == int) {
defaultValue = 0 as T; defaultValue = 0 as T;
} else if (T == bool || T == _typeOf<bool?>()) { } else if (T == bool) {
defaultValue = false as T; defaultValue = false as T; // This ensures bools start correctly
} }
if (defaultValue != null) { if (defaultValue != null) {

View File

@@ -38,12 +38,7 @@ class _AstromicValueWrapperState<T> extends State<AstromicValueWrapper<T>> {
try { try {
_node = widget.controller.node<T>(widget.nodeId, initialValue: widget.initialValue); _node = widget.controller.node<T>(widget.nodeId, initialValue: widget.initialValue);
} catch (_) { } catch (_) {
if (widget.initialValue != null) { throw Exception('Node ${widget.nodeId}: failed to initialize.');
widget.controller.set<T>(widget.nodeId, widget.initialValue as T);
_node = widget.controller.node<T>(widget.nodeId);
} else {
throw Exception('Node ${widget.nodeId} not found or failed to initialize.');
}
} }
if (widget.validators != null) { if (widget.validators != null) {