flutter_form_builder
flutter_form_builder copied to clipboard
FormBuilderDropdown assert error with initialValue
Hi, thanks for sharing source codes! But FormBuilderDropdown has a serious issue, every time when i am trying to update UI with new items, i have a following issue:
assert(items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1,
"There should be exactly one item with [DropdownButton]'s value: "
'$value. \n'
'Either zero or 2 or more [DropdownMenuItem]s were detected '
'with the same value',
),
I have allready removed FormBuilderDropdown, and directly used with same source code DropdownButton, everything is worked like a charm, no problem. By me the problem of FormBuilderDropdown is "initialValue" the "initialValue" may be not updating, i will try to detail research this issue, but issue is exsists 100%.
Can you share your code of the FormBuilderDropdown
that is causing the problem?
This seems to be occurring when the inital value of the dropdown is an empty string, and of course an empty string isn't part of my list for the dropdown. Setting this value to null rather than an empty string fixes the issue.
There are times when my initial value will be empty, because i am using the same form to add/edit. I would expect that if its an empty string it would show the default label text, but it throws the error instead.
If you declare initialValue, then you can't leave it blank.
An easy fix is using a ternary operator ?
to make sure initialValue always has a value. This includes having an empty/null value.
eg:
initialValue: data != null ? data : null
In the case of a FormBuilderDropdown, returning a null
value works.
Something like a FormBuilderFilterChip however requires a list []
.
Empty string might be a valid drop down value, so empty and null must be treated differently
anyone can solve this problem ?
This issue is resolved? If not, please update generic title to specific
This issue still happens in version 7.3.1
Same issue even if the text is not null.
I've since opted out of using this. Found a much better and cleaner solution. Those familiar with angular may feel right at home.
https://pub.dev/packages/reactive_forms
I just resolved my issue.
Assume we use int
as the value of DropdownMenuItem
i.e.
items = [
DropdownMenuItem<int>(
value: 100,
child: Text('Apple'),
),
DropdownMenuItem<int>(
value: 200,
child: Text('Orange'),
)
]
The value of FormBuilderDropdown.initialValue
must also be an int
(100 or 200 or null).
@Chappie74 why i should use ReactiveForm, what addition required feature it gives me? I use Native from builder, and everything seems to be ok. Just for interesting.
@KanybekMomukeyev I can't remember why I switched since it was a year or so ago. But I'm sure it was due to having troubles with this package. You can use whatever works for you. You can also take a read at the docs for reactive forms, maybe you'll find something you'd like.
This issue still happen when I use state management to update item list. DropDownMenuItem still remember the previous values and throw an error
There should be exactly one item with [DropdownButton]'s value: 3. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
If someone can share some minimal example code and steps to reproduce the issue, I really will appreciate
If someone can share some minimal example code and steps to reproduce the issue, I really will appreciate
This issue still happens. (flutter_form_builder: ^7.7.0) I have created gist from my code - https://gist.github.com/WasimMemon/3fa67ad821db2be90582bea3521c8afe Please check and let us know solution for it.