[FIX] Node initialization logic for non-dynamic values.
This commit is contained in:
@@ -38,10 +38,45 @@ class AstromicFormController {
|
||||
|
||||
/// Retrieves a specific [AstromicFieldNode] by its unique [id], creating it if an [initialValue] is provided.
|
||||
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)) {
|
||||
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>()) {
|
||||
final newNode = AstromicFieldNode<T>(
|
||||
initialValue as T,
|
||||
@@ -52,13 +87,14 @@ class AstromicFormController {
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// Standard defaults for primitives
|
||||
T? defaultValue;
|
||||
if (T == String || T == _typeOf<String?>()) {
|
||||
if (T == String) {
|
||||
defaultValue = '' as T;
|
||||
} else if (T == int || T == _typeOf<int?>()) {
|
||||
} else if (T == int) {
|
||||
defaultValue = 0 as T;
|
||||
} else if (T == bool || T == _typeOf<bool?>()) {
|
||||
defaultValue = false as T;
|
||||
} else if (T == bool) {
|
||||
defaultValue = false as T; // This ensures bools start correctly
|
||||
}
|
||||
|
||||
if (defaultValue != null) {
|
||||
|
||||
@@ -38,12 +38,7 @@ class _AstromicValueWrapperState<T> extends State<AstromicValueWrapper<T>> {
|
||||
try {
|
||||
_node = widget.controller.node<T>(widget.nodeId, initialValue: widget.initialValue);
|
||||
} catch (_) {
|
||||
if (widget.initialValue != null) {
|
||||
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.');
|
||||
}
|
||||
throw Exception('Node ${widget.nodeId}: failed to initialize.');
|
||||
}
|
||||
|
||||
if (widget.validators != null) {
|
||||
|
||||
Reference in New Issue
Block a user