-
-
Notifications
You must be signed in to change notification settings - Fork 540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FormBuilderTextField sets field state to '' even though transformer sets return to null #1350
Comments
I've dug into this further and it appears that if the field started out as null and was left null, it works as expected on save - I get a null value and not an empty string. However, if the field is "touched", e.g. I empty a field that has a value or I enter some data and then empty it, the valueTransformer runs and sets the value to null but the currentState.values entry for the field gets set to an empty string at some point between valueTransformer and saveAndValidate. |
same issue for me, and it makes my serialiser fails: even if the transformer correctly returns null, it still gets saved as '', from a quick check I think it take the null as a return value for "do not transform" and it applies the current value, I think this is wrong in terms of logic as it doesn't work with the nullable fields |
Im having the same issue, also i pointed it out on the discussions: |
I have looked through the code and the issue is indeed with how the valueTransformer is implemented. As i am not too familiar with the library and there seem to be some dead code in regards to the valueTransformer I am reluctant to make a pull request but could do so if noone else has the time to look through this. The problem is in every instance where the transformer is used: _transformers[key]?.call(value) ?? value)) This works fine as long as the transformer doesn't transform the value to null. If the transformer however transform the value to null the evaluation will use the original value instead To fix this changing the above code to: _transformers[key] == null ? value : _transformers[key]!(value) Here the transformed value will be used even if it transforms the value to null. |
Does anybody know when this will be available in a new version? |
Hi @andresthinkme! |
Is there an existing issue for this?
Package/Plugin version
9.1.1
Platforms
Flutter doctor
Flutter doctor
Minimal code example
Code sample
Current Behavior
Trying to use FormBuilderTextField to create a field that accepts either an integer or nothing. The int.tryParse call inside valueTransformer returns null if value is an empty string, however the field state gets set to an empty string rather than null. This forces me to transform the transform so that I can get to the needed null.
While experimenting I took the int.tryParse out of the equation by transforming to null no matter what, e.g. valueTransformer: (value) => null. The field state is still changed to ''.
Expected Behavior
If I am intentionally transforming the value to null, I expect the field state to be null and not an empty string. Am I missing a necessary property?
Steps To Reproduce
Incorporate the above field widget sample into a form and try it out.
Aditional information
I know I can work around this by transforming all of the empty integer fields to null on submit, but this should be unnecessary. Please let me know if this can be fixed or if I'm missing something.
The text was updated successfully, but these errors were encountered: