Skip to content
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

ReactiveTextField is being cleared #412

Open
nikolasduda opened this issue Sep 8, 2023 · 5 comments
Open

ReactiveTextField is being cleared #412

nikolasduda opened this issue Sep 8, 2023 · 5 comments

Comments

@nikolasduda
Copy link

nikolasduda commented Sep 8, 2023

When I insert a value into a text field like
form.control('some_control').value = 'some_text';
I click on the text field, start editing the value, then the entire field is cleared
What i can do to avoid this

@isaacsuazo7
Copy link

isaacsuazo7 commented Sep 8, 2023

Hi
You can try assigning the value like this: form.control('some_control').updateValue('some_text');
Greetings.

@joanpablo
Copy link
Owner

Hi @OVERENSAL,

It sounds like you might have declared the FormGroup inside a Stateless Widget and each time the widget is destroyed and rebuilt you start with a fresh formGroup.

There are 3 different approaches to avoid that:
1- Use a state management library and declare the FormGroup in the State(controller, Bloc, provider, whatever the name is depending on the library you are using)

2- Use a Stateful widget and declare your formGroup in the State.

3- Use the ReactiveFormBuilder (it is a Stateful widget by itself)

@nikolasduda
Copy link
Author

nikolasduda commented Sep 11, 2023

Thank you for attention @joanpablo
I use third variant, but right now i detect that thats not working with inputFormatters
I use this MaskTextInputFormatter from mask_text_input_formatter: 2.4.0
MaskTextInputFormatter( mask: '#### #### #### ####', filter: {'#': RegExp(r'[0-9]')}, ),

@smallstone719
Copy link

That's correct! If I enter a value from the keyboard, there are no issues. However, when I initialize the value using formGroup.control(CompanyFieldName.phone).value = phoneMaskFormatter.maskText('1123456789'), all the characters get cleared when I press the delete button on the keyboard or start editing the value.

final phoneMaskFormatter = MaskTextInputFormatter(
mask: '(###) ###-####',
filter: {"#": RegExp(r'[0-9]')},
type: MaskAutoCompletionType.lazy,
);

@smallstone719
Copy link

smallstone719 commented Oct 14, 2023

My problem has been solved:

ReactiveValueListenableBuilder<String>(
          formControlName: formControlName,
          builder: (context, control, _) {
            final controlValue = control.value;
            control.value = phoneMaskFormatter.maskText(control.value ?? '');
            return ReactiveTextField<String>(
                   // Not use this
                   //inputFormatters: inputFormatters,
                   ...
            )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants