Skip to content

Commit

Permalink
Fix for issue 1350 with some changes to tests. This fix makes it so t…
Browse files Browse the repository at this point in the history
…hat valueTransformer can transform values to null.
  • Loading branch information
BrottarBasse committed Aug 28, 2024
1 parent 4d27723 commit eb5920f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
22 changes: 16 additions & 6 deletions lib/src/form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,24 @@ class FormBuilderState extends State<FormBuilder> {
/// Get all fields of form.
FormBuilderFields get fields => _fields;

Map<String, dynamic> get instantValue =>
Map<String, dynamic>.unmodifiable(_instantValue.map((key, value) =>
MapEntry(key, _transformers[key]?.call(value) ?? value)));
Map<String, dynamic> get instantValue => Map<String, dynamic>.unmodifiable(
_instantValue.map(
(key, value) => MapEntry(
key,
_transformers[key] == null ? value : _transformers[key]!(value),
),
),
);

/// Returns the saved value only
Map<String, dynamic> get value =>
Map<String, dynamic>.unmodifiable(_savedValue.map((key, value) =>
MapEntry(key, _transformers[key]?.call(value) ?? value)));
Map<String, dynamic> get value => Map<String, dynamic>.unmodifiable(
_savedValue.map(
(key, value) => MapEntry(
key,
_transformers[key] == null ? value : _transformers[key]!(value),
),
),
);

dynamic transformValue<T>(String name, T? v) {
final t = _transformers[name];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class FormBuilderFieldState<F extends FormBuilderField<T>, T>
(_formBuilderState?.initialValue ??
const <String, dynamic>{})[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;
Expand Down
24 changes: 14 additions & 10 deletions test/src/form_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -54,17 +58,17 @@ 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;
await tester.pump();

// 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);
},
);

Expand Down Expand Up @@ -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;
Expand All @@ -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);
},
);
});
Expand Down

0 comments on commit eb5920f

Please sign in to comment.