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

FormBuilderDropdown onChanged fires twice during form Reset #1371

Closed
5 of 7 tasks
Adam-Langley opened this issue Feb 22, 2024 · 2 comments · Fixed by #1451
Closed
5 of 7 tasks

FormBuilderDropdown onChanged fires twice during form Reset #1371

Adam-Langley opened this issue Feb 22, 2024 · 2 comments · Fixed by #1451
Labels
bug Something isn't working

Comments

@Adam-Langley
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.2.1

Platforms

  • Android
  • iOS
  • Linux
  • MacOS
  • Web
  • Windows

Flutter doctor

Flutter doctor

Minimal code example

Code sample
FormBuilder(
    child: Column(
      children: [
        Builder(
          builder: (context) {
            return ElevatedButton(onPressed: (){
              FormBuilder.of(context)!.reset();
            }, child: Text('reset'));
          }
        ),
        FormBuilderDropdown(
          name: 'test', 
          initialValue: 'A', 
          items: [
            DropdownMenuItem(child: Text('A'), value: 'A'),
            DropdownMenuItem(child: Text('B'), value: 'B')
          ],
          onChanged: (value) {
            print('Changed value to $value');
          },
        ),
      ],
    ),
  ),

Current Behavior

When resetting the form, the dropdowns "onChange" handler will fire twice in quick succession - one setting it to the "initialValue", then a second setting it back to the value it was before the reset button was clicked (essentially undoing the reset).

Expected Behavior

When resetting the form, the "onChange" should fire only once, and the passed value should be the "initialValue".

Steps To Reproduce

Using the above code snippet.

  1. Run the code
  2. Click the dropdown and change its value to "B"
  3. Press the "reset" button

Note that it will not reset, but stay as "B", and the debug console will read

flutter: Changed value to A
flutter: Changed value to B

Aditional information

No response

@Adam-Langley Adam-Langley added the bug Something isn't working label Feb 22, 2024
@Adam-Langley
Copy link
Author

Adam-Langley commented Feb 22, 2024

Additional information:

In my actual app (I'm assuming this is what's going on inside the demo code I provided too) - I have 4 FormBuilder fields.
2 * FormBuilderTextField
2 * FormBuilderDropdown

When I breakpoint the onChange, I can see that the form actually contains 6 field state objects - 2 for the text fields, and 4 for the dropdowns... it looks like for each dropdown there is a "FormBuilderDropdownState" and a "DropdownButtonFormFieldState".

When 'reset' is called on the FormBuilderDropdownState, it correctly fires onChanged and passes the initialValue.
However, when 'reset' is then called on the DropdownButtonFormFieldState this is when it resets it back to its current value.

I suspect this second state class isn't supposed to be in the fields collection - as it's just a double-up, one that breaks the functionality at that.

image

@RanjanKiran707
Copy link

RanjanKiran707 commented Sep 15, 2024

Additional information:

In my actual app (I'm assuming this is what's going on inside the demo code I provided too) - I have 4 FormBuilder fields. 2 * FormBuilderTextField 2 * FormBuilderDropdown

When I breakpoint the onChange, I can see that the form actually contains 6 field state objects - 2 for the text fields, and 4 for the dropdowns... it looks like for each dropdown there is a "FormBuilderDropdownState" and a "DropdownButtonFormFieldState".

When 'reset' is called on the FormBuilderDropdownState, it correctly fires onChanged and passes the initialValue. However, when 'reset' is then called on the DropdownButtonFormFieldState this is when it resets it back to its current value.

I suspect this second state class isn't supposed to be in the fields collection - as it's just a double-up, one that breaks the functionality at that.

image

Yeah but DropdownButtonFormField also extends FormField, In FormField's build function we are registering this field. So even this gets registered

If we comment out the reset function for DropdownButtonFormFieldState it works.

Also if we replace DropdownButtonFormField with DropdownButton it works fine, since DropdownButton is not a form field, it will not register it's field. But DropdownButton doesn't have a decoration parameter. I think we should go with this as we're unnecessarily creating an extra field if we use DropdownButtonFormField.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants