Skip to content

Commit

Permalink
Merge pull request #1423 from BrottarBasse/fix-issue-1350
Browse files Browse the repository at this point in the history
fix: #1350 valueTransformer transform values to null
  • Loading branch information
deandreamatias authored Sep 17, 2024
2 parents 6ed0144 + d6fa69a commit 70968df
Show file tree
Hide file tree
Showing 3 changed files with 32 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
3 changes: 2 additions & 1 deletion lib/src/form_builder_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ 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 70968df

Please sign in to comment.