diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index 7c524f8..cc9a205 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -82,28 +82,26 @@ class AstromicFormController extends FormController { /// Prepare a hosted value. void prepareValue(String id, bool isRequired) { - return _hostedValues.addEntries(>[MapEntry(id, (null, isRequired))]); + if (!_hostedValues.keys.toList().contains(id)) { + return _hostedValues.addEntries(>[MapEntry(id, (null, isRequired))]); + } } /// Set the value of a hosted state variable using it's ID. void setValue(String id, T data, {bool isRequired = false}) { - if (!_hostedValues.keys.toList().contains(id)) { - prepareValue(id, isRequired); - } + prepareValue(id, isRequired); _hostedValues[id] = (data, isRequired); _hostedValueValidationStreamController.add((id, false)); } /// Get the value of a hosted state variable using it's ID. T? getValue(String id) { - if (_hostedValues.keys.toList().contains(id)) { - if (_hostedValues[id]?.$1 is T?) { - return _hostedValues[id]?.$1; - } else { - throw FlutterError('Value found but is not of the type $T, it\'s of type ${_hostedValues[id]?.$1.runtimeType}'); - } + prepareValue(id, false); + + if (_hostedValues[id]?.$1 is T?) { + return _hostedValues[id]?.$1; } else { - return null; + throw FlutterError('Value found but is not of the type $T, it\'s of type ${_hostedValues[id]?.$1.runtimeType}'); } }