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]: Not reverting to initial value upon form reset #1402

Closed
2 of 7 tasks
kris175 opened this issue May 30, 2024 · 5 comments · Fixed by #1451
Closed
2 of 7 tasks

[FormBuilderDropdown]: Not reverting to initial value upon form reset #1402

kris175 opened this issue May 30, 2024 · 5 comments · Fixed by #1451
Labels
bug Something isn't working

Comments

@kris175
Copy link

kris175 commented May 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.3.0

Platforms

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

Flutter doctor

Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-arm64, locale en-AU)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] IntelliJ IDEA Community Edition (version 2023.2.2)
[✓] VS Code (version 1.89.1)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

Minimal code example

Code sample
class MyForm extends StatefulWidget {
  const MyForm({super.key});

  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: FormBuilder(
        key: _formKey,
        child: Column(
          children: <Widget>[
            FormBuilderDropdown(
              name: 'dropdown',
              decoration: const InputDecoration(
                labelText: 'Select Option',
              ),
              // hint: Text('Select Option'), // Optional
              items: const [
                DropdownMenuItem(
                  value: 'Option 1',
                  child: Text('Option 1'),
                ),
                DropdownMenuItem(
                  value: 'Option 2',
                  child: Text('Option 2'),
                ),
                DropdownMenuItem(
                  value: 'Option 3',
                  child: Text('Option 3'),
                ),
              ],
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                print("BEFORE RESET");
                print(_formKey.currentState?.value);
                _formKey.currentState?.reset();
                print("AFTER RESET");
                print(_formKey.currentState?.value);
              },
              child: const Text('Reset'),
            ),
          ],
        ),
      ),
    );
  }
}

Current Behavior

The dropdown field is not reverting to the initial value upon form reset. It instead retains the value that has been selected prior to reset.

Expected Behavior

Dropdown field should be reset to initial value when the form is reset

Steps To Reproduce

  1. Run app
  2. Dropdown field has initial value as null. Select an option
  3. Press reset button

Aditional information

flutter: BEFORE RESET
flutter: {dropdown: Option 2}
flutter: AFTER RESET
flutter: {dropdown: Option 2}
@kris175 kris175 added the bug Something isn't working label May 30, 2024
@kris175
Copy link
Author

kris175 commented May 31, 2024

Could be related to this issue - #1389

@thandal
Copy link

thandal commented Sep 2, 2024

I think this is just a bug. When reset() is called, onChange gets called twice for FormBuilderDropdown widgets: first with the correct (original) value, then with the incorrect (current value). Net result: the value doesn't get reset to the original value. (flutter_form_builder: ^9.4.1)

@thandal
Copy link

thandal commented Sep 2, 2024

... which is the same issue mentioned here: #1371

@deandreamatias
Copy link
Collaborator

I test this and using reset by field, _formKey.currentState?.fields['dropdown']?.reset(), works fine.
I debugged why Dropdown don't reset but I don't have a conclusion. The only thing that I found is that on form.dart (from Flutter core), had two Dropdown states. One on FormBuilderDropdown and other for DropdownButtonFormFieldState. Maybe the bug is related with that

image

@deandreamatias
Copy link
Collaborator

While I writing the previous comment, I notice that I used DropdownButtonFormField instead DropdownButton.
This created a conflict between the FormBuilder state (package) and the Form Flutter state (core).

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.

3 participants