flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

FormBuilderDropdown assert error with initialValue

Open KanybekMomukeyev opened this issue 3 years ago • 14 comments

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%.

KanybekMomukeyev avatar Jan 14 '21 12:01 KanybekMomukeyev

Can you share your code of the FormBuilderDropdown that is causing the problem?

andrewzakhartchouk avatar Jan 22 '21 01:01 andrewzakhartchouk

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.
image
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.
image

Chappie74 avatar Jan 22 '21 15:01 Chappie74

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 [].

andrewzakhartchouk avatar Jan 25 '21 01:01 andrewzakhartchouk

Empty string might be a valid drop down value, so empty and null must be treated differently

derolf avatar Feb 21 '21 19:02 derolf

anyone can solve this problem ?

YouSour avatar Jul 21 '21 04:07 YouSour

This issue is resolved? If not, please update generic title to specific

mdeandrea-mrmilu avatar May 10 '22 08:05 mdeandrea-mrmilu

This issue still happens in version 7.3.1

deandreamatias avatar Jun 28 '22 06:06 deandreamatias

Same issue even if the text is not null.

tonypottera24 avatar Jul 07 '22 00:07 tonypottera24

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

Chappie74 avatar Jul 07 '22 00:07 Chappie74

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).

tonypottera24 avatar Jul 07 '22 01:07 tonypottera24

@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 avatar Jul 07 '22 01:07 KanybekMomukeyev

@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.

Chappie74 avatar Jul 07 '22 03:07 Chappie74

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

PongsiriTK avatar Aug 12 '22 13:08 PongsiriTK

If someone can share some minimal example code and steps to reproduce the issue, I really will appreciate

deandreamatias avatar Aug 12 '22 14:08 deandreamatias

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.

WasimMemon avatar Oct 27 '22 16:10 WasimMemon