flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

How to preprocess the initialValue?

Open Cinux-Chosan opened this issue 4 years ago • 3 comments

valueTransformer: (value) => int.tryParse(value) will convert the value to a number, but next time when I open the form the initialValue is a number, this will lead an error: Expected a value of type 'String?', but got one of type 'int'

Is there an initialValueTransformer or some way I can use to convert the initialValue to a String that the FormBuilderTextFieldexpected?

Cinux-Chosan avatar Apr 07 '21 17:04 Cinux-Chosan

Just use the .toString() method.

int numbers = 12345;

initialValue: numbers.toString();

jaxnz avatar Apr 18 '21 18:04 jaxnz

@jaxnz Thanks, but .toString() is not what I want. If the valueTransformer is valueTransformer: (value) => int.parse(value) /100, so I need something can perform the reverse process initialValueTransformer: (value) => (value * 100).toString(), not only just .toString(). But It seems that there is no such property.

Cinux-Chosan avatar Apr 27 '21 03:04 Cinux-Chosan

@jaxnz Thanks, but .toString() is not what I want. If the valueTransformer is valueTransformer: (value) => int.parse(value) /100, so I need something can perform the reverse process initialValueTransformer: (value) => (value * 100).toString(), not only just .toString(). But It seems that there is no such property.

it would be helpful and clean to have such property , as a workaround , in your controller transform all integers to strings before you set initialValue , this package should have a FormBuilderNumberField out of the box , as it is very common .

takifouhal avatar Oct 09 '21 12:10 takifouhal