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]: silently ignores an unknown initialValue #1363

Open
7 tasks done
grundid opened this issue Feb 6, 2024 · 2 comments
Open
7 tasks done

[FormBuilderDropdown]: silently ignores an unknown initialValue #1363

grundid opened this issue Feb 6, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@grundid
Copy link
Contributor

grundid commented Feb 6, 2024

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

Latest Flutter

Minimal code example

 FormBuilderDropdown(
                      name: 'weekday',
                      decoration: InputDecoration(
                        labelText: 'Weekday',
                      ),
                      initialValue: DateTime.sunday,
                      allowClear: false,
                      validator: FormBuilderValidators.compose(
                        [FormBuilderValidators.required()],
                      ),
                      items: [
                        DropdownMenuItem<int>(
                          value: DateTime.monday,
                          child: Text("Monday"),
                        )
                      ])

Current Behavior

The dropdown shows an empty entry but still keeps the value in the state. When the user saves the form the value is returned as a valid selection.

Expected Behavior

The dropdown should fail as per design if an unknown option is given.

Steps To Reproduce

see minimal code example

Aditional information

No response

@grundid grundid added the bug Something isn't working label Feb 6, 2024
@deandreamatias deandreamatias changed the title [Field name or General]: FormBuilderDropdown silently ignores an unknown initialValue [FormBuilderDropdown]: silently ignores an unknown initialValue Dec 18, 2024
@fatyga
Copy link
Contributor

fatyga commented Dec 21, 2024

For first, there is a problem with your code example. It assumes, that underlying DropdownButton can represent a value outside the available ones. It is in a state, that user can't even select.

In case of DropdownButtonFormField from material library, assertion will fire up to show, that you are breaking the rules. Try this:

DropdownButtonFormField<int>(
                hint: Text('Weekday'),
                value: DateTime.sunday,
                items: [
                  DropdownMenuItem<int>(
                    value: DateTime.monday,
                    child: Text("Monday"),
                  ),
                ],
                onChanged: (int? value) {})

It will of course force you to provide correct value and list of values. Then everything will work fine.

I've investigated current FormBuilderDropdown implementation, and it somehow skips assertion on first build. Next ones will fire up an assertion included in didUpdateWidget method.

I think the problem with current implementation is the assumption, that argument provided to value can be possibly outside available values.

@deandreamatias
Copy link
Collaborator

Hi @grundid
Maybe this #1451 had something related with this?
Can test with main branch

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

No branches or pull requests

3 participants