From eb5920f964604884c39f7d164e321c818dad752b Mon Sep 17 00:00:00 2001 From: BrottarBasse Date: Wed, 28 Aug 2024 14:15:31 +0200 Subject: [PATCH 1/2] Fix for issue 1350 with some changes to tests. This fix makes it so that valueTransformer can transform values to null. --- lib/src/form_builder.dart | 22 ++++++++++++++++------ lib/src/form_builder_field.dart | 2 +- test/src/form_builder_test.dart | 24 ++++++++++++++---------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/src/form_builder.dart b/lib/src/form_builder.dart index 28333c5b3..0734e4514 100644 --- a/lib/src/form_builder.dart +++ b/lib/src/form_builder.dart @@ -170,14 +170,24 @@ class FormBuilderState extends State { /// Get all fields of form. FormBuilderFields get fields => _fields; - Map get instantValue => - Map.unmodifiable(_instantValue.map((key, value) => - MapEntry(key, _transformers[key]?.call(value) ?? value))); + Map get instantValue => Map.unmodifiable( + _instantValue.map( + (key, value) => MapEntry( + key, + _transformers[key] == null ? value : _transformers[key]!(value), + ), + ), + ); /// Returns the saved value only - Map get value => - Map.unmodifiable(_savedValue.map((key, value) => - MapEntry(key, _transformers[key]?.call(value) ?? value))); + Map get value => Map.unmodifiable( + _savedValue.map( + (key, value) => MapEntry( + key, + _transformers[key] == null ? value : _transformers[key]!(value), + ), + ), + ); dynamic transformValue(String name, T? v) { final t = _transformers[name]; diff --git a/lib/src/form_builder_field.dart b/lib/src/form_builder_field.dart index e73d6f365..7cdeebcb2 100644 --- a/lib/src/form_builder_field.dart +++ b/lib/src/form_builder_field.dart @@ -88,7 +88,7 @@ class FormBuilderFieldState, T> (_formBuilderState?.initialValue ?? const {})[widget.name] as T?; - dynamic get transformedValue => widget.valueTransformer?.call(value) ?? value; + dynamic get transformedValue => widget.valueTransformer == null ? value : widget.valueTransformer!(value); @override String? get errorText => super.errorText ?? _customErrorText; diff --git a/test/src/form_builder_test.dart b/test/src/form_builder_test.dart index 53c0f9052..db9e59e40 100644 --- a/test/src/form_builder_test.dart +++ b/test/src/form_builder_test.dart @@ -39,10 +39,14 @@ void main() { ), ); + // Write an input resulting in a null value after transformation + formFieldDidChange(testWidgetName, 'a'); + expect(formInstantValue(testWidgetName), isNull); + // Write an input and test the transformer formFieldDidChange(testWidgetName, '1'); - expect(int, formInstantValue(testWidgetName).runtimeType); - expect(1, formInstantValue(testWidgetName)); + expect(formInstantValue(testWidgetName).runtimeType, int); + expect(formInstantValue(testWidgetName), 1); // Remove the dynamic field from the widget tree final _DynamicFormFieldsState dynamicFieldState = @@ -54,8 +58,8 @@ void main() { // With the field unregistered, the form does not have its transformer // but it still has its value, now recovered as type String - expect(String, formInstantValue(testWidgetName).runtimeType); - expect('1', formInstantValue(testWidgetName)); + expect(formInstantValue(testWidgetName).runtimeType, String); + expect(formInstantValue(testWidgetName), '1'); // Show and recreate the field's state dynamicFieldState.show = true; @@ -63,8 +67,8 @@ void main() { // The transformer is registered again and with the internal value that // was kept, it's expected an int of value 1 - expect(int, formInstantValue(testWidgetName).runtimeType); - expect(1, formInstantValue(testWidgetName)); + expect(formInstantValue(testWidgetName).runtimeType, int); + expect(formInstantValue(testWidgetName), 1); }, ); @@ -102,8 +106,8 @@ void main() { // With the field unregistered, the form does not have its transformer, // and since the value was cleared, neither its value - expect(Null, formInstantValue(testWidgetName).runtimeType); - expect(null, formInstantValue(testWidgetName)); + expect(formInstantValue(testWidgetName).runtimeType, Null); + expect(formInstantValue(testWidgetName), isNull); // Show and recreate the field's state dynamicFieldState.show = true; @@ -112,8 +116,8 @@ void main() { // A new input is needed to get another value formFieldDidChange(testWidgetName, '2'); await tester.pump(); - expect(int, formInstantValue(testWidgetName).runtimeType); - expect(2, formInstantValue(testWidgetName)); + expect(formInstantValue(testWidgetName).runtimeType, int); + expect(formInstantValue(testWidgetName), 2); }, ); }); From db740444232c7d2b05103ea1244557e48e92552b Mon Sep 17 00:00:00 2001 From: BrottarBasse Date: Mon, 16 Sep 2024 14:46:09 +0200 Subject: [PATCH 2/2] Fix for issue 1350 with some changes to tests. This fix makes it so that valueTransformer can transform values to null. --- lib/src/form_builder_field.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/form_builder_field.dart b/lib/src/form_builder_field.dart index 7cdeebcb2..043afffa7 100644 --- a/lib/src/form_builder_field.dart +++ b/lib/src/form_builder_field.dart @@ -88,7 +88,8 @@ class FormBuilderFieldState, T> (_formBuilderState?.initialValue ?? const {})[widget.name] as T?; - dynamic get transformedValue => widget.valueTransformer == null ? value : widget.valueTransformer!(value); + dynamic get transformedValue => + widget.valueTransformer == null ? value : widget.valueTransformer!(value); @override String? get errorText => super.errorText ?? _customErrorText;