74

I am trying to create a dropdown button in Flutter. I am getting a List from my database then I pass the list to my dropdownButton everything works the data is shown as intended but when I choose an element from it I get this error:

There should be exactly one item with [DropdownButton]'s value: Instance of 'Tag'. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null || items.isEmpty || value == null ||
          items.where((DropdownMenuItem<T> item) {
            return item.value == value;
          }).length == 1'

I tried setting DropdownButton value to null it works but then I can't see the chosen element.

Here is my code:

FutureBuilder<List<Tag>>(
    future: _tagDatabaseHelper.getTagList(),
    builder: (BuildContext context, AsyncSnapshot<List<Tag>> snapshot) {
      if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(),
        );
      }
      return ListView(
        children: <Widget>[
          SizedBox(
            height: MediaQuery.of(context).size.height * 0.2,
          ),
          Container(
            margin: EdgeInsets.symmetric(
                horizontal: MediaQuery.of(context).size.width * 0.07),
            child: Theme(
              data: ThemeData(canvasColor: Color(0xFF525A71)),
              child: DropdownButton<Tag>(
                value: _selectedTag,
                isExpanded: true,
                icon: Icon(
                  Icons.arrow_drop_down,
                  size: 24,
                ),
                hint: Text(
                  "Select tags",
                  style: TextStyle(color: Color(0xFF9F9F9F)),
                ),
                onChanged: (value) {
                  setState(() {
                    _selectedTag = value;
                  });
                },
                items: snapshot.data.map((Tag tag) {
                  return DropdownMenuItem<Tag>(
                    value: tag,
                    child: Text(
                      tag.tagTitle,
                      style: TextStyle(color: Colors.white),
                    ),
                  );
                }).toList(),
                value: _selectedTag,
              ),
            ),
          ),

I used futureBuilder to get my List from database.

Abdelbaki Boukerche
  • 1,602
  • 1
  • 8
  • 20
  • 1
    Have you tried hardcoding a value there? String value = "some val"; before your FutureBuilder and assigning that to _value = value; – van Mar 03 '20 at 15:19
  • @van Yes same problem. – Abdelbaki Boukerche Mar 03 '20 at 15:37
  • Just to add to what others have already mentioned, not only does the default value need to match ONE of the values in your list, if it is a String, the case must also match. If you have NIGERIA in your list, and that's the default value, the default must also be NIGERIA, not Nigeria. – Ojonugwa Jude Ochalifu Oct 20 '21 at 06:17

22 Answers22

116

Well, since no problem has an exact same solution. I was facing the same issue with my code. Here is How I fixed this.

CODE of my DropdownButton:

DropdownButton(
   items: _salutations
         .map((String item) =>
             DropdownMenuItem<String>(child: Text(item), value: item))
         .toList(),
    onChanged: (String value) {
       setState(() {
         print("previous ${this._salutation}");
         print("selected $value");
         this._salutation = value;
            });
          },
     value: _salutation,
),

The Error

In the code snippet below, I am setting the state for a selection value, which is of type String. Now problem with my code was the default initialization of this selection value. Initially, I was initializing the variable _salutation as:

String _salutation = ""; //Notice the empty String.

This was a mistake!

Initial selection should not be null or empty as the error message correctly mentioned.

'items == null || items.isEmpty || value == null ||

And hence the crash:

crash_message

Solution
Initialize the value object with some default value. Please note that the value should be the one of the values contained by your collection. If it is not, then expect a crash.

  String _salutation = "Mr."; //This is the selection value. It is also present in my array.
  final _salutations = ["Mr.", "Mrs.", "Master", "Mistress"];//This is the array for dropdown
sud007
  • 5,824
  • 4
  • 56
  • 63
  • 10
    Do note default selection value should be present in the array. – Ponmudi VN Feb 25 '21 at 16:34
  • 1
    This solved my problem as of Sep 2021 – Wilfred Almeida Sep 20 '21 at 08:51
  • it can't work if data was dynamic from server. how could you tell me how it's work if you have data dynamic? – Michael Fernando Nov 23 '21 at 03:17
  • @MichaelFernando I think in that is a valid question. Did you try initialising the UI only once the data from server has loaded? Because then, you will have either an empty initialiser or the first item in the list. – sud007 Nov 24 '21 at 08:56
  • 1
    finally, it's has works. because my func dropdown i set inside widget build. so that, it makes always load data in first time. so, i change position this function at outside of widget build and works normally. thank you for explain me about it @sud007 – Michael Fernando Nov 24 '21 at 10:19
  • But what if I want the drop down value to be empty first, therefore forcing the user to select a choice? I don't want a default selected value – MobileMon Feb 11 '22 at 13:15
  • @MobileMon to resolve this there could be 2 solutions. You can use the solution shared by Nuts below, Or to set a default selection, wait for API response, get the list, add default value at index ZERO and then initialise the UI with that data. Not a clean one but will serve you purpose. – sud007 Feb 13 '22 at 12:56
  • 1
    My code was missing the setState, now it's working. Thanks. – Paulo Pedroso Jun 29 '22 at 01:23
  • @sud007 Hi I'm facing the same issue, so in my array for dropdown, there's a duplicate value like ['Mr.','Mr'], so if i choose one ot those i got error like you. and i didn't want to set initialValue or fill the selection value. do you have another solution?. tq – MNFS Jul 28 '22 at 06:56
  • @MNFS I think as a literal string comparison `Mr.` != `Mr` not sure how that is happening. But again, maybe introduce the "Select Salutation" as an initial item in the list and in the dropdown too, but that's probably upto your usecase. – sud007 Aug 09 '22 at 14:42
  • 2
    Also if you're using a custom class then we've to override == operator and hashcode in order to work correctly or use a package like Equatable – Vinoth Vino Jan 03 '23 at 16:31
  • 1
    When i removed the value of DropdownButton it worked – Breno AllenCS Apr 06 '23 at 19:42
47

Might also get this error if trying to set value of dropdown with a class instance;

  var tag1 = Tag();
  var tag2 = Tag();
  print(tag1 == tag2); // prints false, dropwdown computes that value is not present among dropdown options

To solve this override operator ==:

class Tag{
 String name = "tag";

  @override
  bool operator ==(Object other) => other is Tag && other.name == name;

  @override
  int get hashCode => name.hashCode;
}

or use https://pub.dev/packages/equatable lib

class Tag extends Equatable{
 String name = "tag";

  @override
  List<Object> get props => [name];
}
Adelina
  • 10,915
  • 1
  • 38
  • 46
20

I had the same problem. The solution is simple: you have to be sure that the String that is your default dropdownvalue is contained in the list that you want to use in your dropdownmenu. If you wanted to, let’s say, use a list from an api, you should be sure to know at least one value of that list, so that you could assign it to the variable that is your default dropdownvalue.

Here I want display a list that I obtain from an api. In order to not obtain the error, I set my defaultdropdownvalue with the name ‘Encajes’ that is one of the existing categories that my list contains.

String dropdownValue = "Encajes";

    items: categoriesString
    .map<DropdownMenuItem<String>>((String value) {
  return DropdownMenuItem<String>(
    value: value,
    child: Text(value),
  );
}).toList(),
Iván Yoed
  • 3,878
  • 31
  • 44
  • defalut value not worked it still gives error. – s.j Mar 01 '21 at 12:15
  • "Failed assertion: line 1411 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1'" geeting same error which getting while changeing dropdown item. – s.j Mar 02 '21 at 04:55
  • Your list is empty, so that means that, even if you're setting the first item correctly, somehow your widget seems to try to be rendered before your list has any values. I would try to initialize my list like List myList = []. In that way you make sure your app doesn't crash before it gets the members. I haven't seen your code, just trying to guess. – Iván Yoed Mar 02 '21 at 04:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229375/discussion-between-s-j-and-ivan-yoed). – s.j Mar 02 '21 at 05:24
10

Some of the answers on this thread will definitely help you resolve the issue. But if your DropdownButton is dealing with custom Objects (Tag class in this case) is important to clarify why this issue occurs in the first place and what the DropdownButton expects from you.

To give you a little background on the issue it is important to understand how two instances of dart objects are compared.

You will very likely not see the above error if your DropdownButton is dealing with a List of int, String, bool, etc.

This is because you can directly compare primitive types and you would get the expected result.

for instance

int x = 5;
int z = 10;
int y = 5;
String foo= 'hello';
String bar = 'hello; 

x == z; // false
x == y; // true
foo == bar; // true

But when dealing with Custom Objects you have to be extra careful and you must ensure you override the "==" operator so that dart knows how to compare instances of your custom object. By default, two objects are equal if they are of the same instance.

consider the Tag class,

class Tag{
final String name;
final String code;

Tag({this.name,this.code});
}
final tag1 = Tag(name:'foo', code: 'hello');
final tag2 = Tag(name:'foo', code: 'hello');
Tag tag3 = tag1;

when you compare tag3==tag1 dart would return true as expected, But when you compare tag1 == tag2, the dart would return false, since both objects are not of the same instance.

So to deal with this issue you need to override the == operator as and modify your Tag class as shown below

class Tag{
final String name;
final String code;

Tag({this.name,this.code});

@override
  bool operator ==(Object other){
      return identical(this, other) ||
        (other.runtimeType == runtimeType &&
        other is Tag &&
        other.name == name &&
        other.code == code
   }
}

Now when you compare tag1 ==tag2 it would return true.

This is documented in the official docs here https://dart.dev/guides/language/effective-dart/design#equality

Coming to the DropdownButton error it expects

  1. items is not null
  2. items is not empty
  3. value is not null
  4. value must be present only once in items

Point 4 would fail if you are using Custom objects without overriding the == operator and hence you would get the above error.

TLDR;

So to deal with the error, ensure the above 4 points satisfy and override the == operator so that dart can compare instances of your Tag class as you would expect.

Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
6

Code of my dropdown

child: DropdownButton(
      items: _currencies.map((String value) {
        return DropdownMenuItem<String>(
          child: Text(value),
          value: value,
        );
      }).toList(),
      value: 'Rupees',
      onChanged: (String newValueSelected) {
        // Your code to execute, when a menu item is selected from 
        dropdown
      },
))
var _currencies = ['Rupee','Dollar','Pound'];

I faced same error because the value in the dropdown code block is not matching with any of the fields in _currencies

gogaz
  • 2,323
  • 2
  • 23
  • 31
Phani
  • 84
  • 1
  • 2
4

just make the tag class extend from Equatable and pass the attributes to the props.. this did the trick for me.

class Tag extends Equatable{
  String id;
  String name;

  Tag(this.id, this.name);

  @override
  List<Object> get props => [id,name];

}
Abdulmalek Dery
  • 996
  • 2
  • 15
  • 39
3

The exact answer is: keep "value" null before user selection:

String selectedValue = '';

And in the DropdownButton2 Widget:

...
value: selectedValue.isEmpty ? null : selectedValue,
...

It says if selectedValue is empty then give null but when user select a value then give selectedValue

Rana Hyder
  • 363
  • 2
  • 10
2

I have had the same issue and surprisingly, there were duplicates in my list of items which were being fetched from a remote DB.

Each time I fetched the data from the server (when a new app user logged in), the data had no duplicates but the same data was being added to the list multiple times because I was logging in multiple users on the same device. Maybe your bug is something similar.

So, make sure you remove any duplicates in the snapshot.data before setting them as items of the DropDownButton.

DK250
  • 1,064
  • 13
  • 11
2

i had the same Error and my default value was not present in the listItems was mapping in the Dropdown Button as :

String defaultvalue = 'selectCategorie'

const List<String> Subcategories = ['category 1','category 2','category 3'...];

Had to Change to this :-

String defaultvalue = 'selectCategorie';

const List<String> Subcategories = ['selectCategorie','category 1','category 2','category 3'...];

now when you pass the defaultvalue in the DropdownButton no errors

DropdownButton (
  item:[]
 onChanged: (String values){
   print(values);
setState(() {
defaultValue = values;
});
},
value: defaultValue,
)
Nsamba Isaac
  • 357
  • 4
  • 14
1

So I found a solution.

I created empty List to hold my Tag objects.

List<Tag> _tagList = [];

Then, in my initState i assigned the list i get from database to the previous List

     @override
    void initState() {
    super.initState();
    _tagDatabaseHelper.getTagList().then((foo) {
      setState(() {
        _tagList = foo;
      });
    });
  }

Finally My DropdownButton code :

DropdownButton<Tag>(
            isExpanded: true,
            icon: Icon(
              Icons.arrow_drop_down,
              size: 24,
            ),
            hint: Text(
              "Select tags",
              style: TextStyle(color: Color(0xFF9F9F9F)),
            ),
            items: _tagList.map((foo) {
              return DropdownMenuItem(
                value: foo,
                child: Text(foo.tagTitle),
              );
            }).toList(),
            onChanged: (value) {
              setState(() {
                _selectedTag = value;
              });
            },
            value: _selectedTag,
          ),
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
Abdelbaki Boukerche
  • 1,602
  • 1
  • 8
  • 20
  • I don't see any issue with the code for `DropdownButton` here and I am doing the same. But doesn't seem to work for me. Just crashes with the same error! If I remove the `value:_assignedString` then this works fine, but then DropdownButton doesn't display the selected value ofcourse. Still looking for a solution. – sud007 Apr 25 '20 at 12:07
  • I solved my issue. @Abdelbaki's issue as well is because of the same reason but since you were fetching async data, it may be a cause but actually it was the `value` attribute getting a `null` or and empty value. – sud007 Apr 25 '20 at 15:39
1

I used a trick. The selected item make as first index item in the list .So when changing item at every time remove the item from list and reinsert the item as first item in the list . Please refer the below code. Here iam using Object as the drop down item and the widget i make it as extracted function. and also before calling the dropDownButton function make

//items list like below

 List<LeaveType> items = [
 (id=1,name="Sick"),
 (id=2,name="Paid")
 ]

selectedLeave = null;

Row leaveTypeDropDown(StateSetter setCustomState, List<LeaveType> items) {
    if(selectedLeave != null){
      items.remove(selectedLeave);
      items.insert(0, selectedLeave);
    }
    return Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children : [
              text("Select Leave Type",textSize: 15),
              Padding(padding: const EdgeInsets.all(5)),
              Expanded(
                child: Container(
                  padding: const EdgeInsets.only(left: 10.0, right: 10.0),
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.black,width: 1),
                    borderRadius: const BorderRadius.all(Radius.circular(10.0)),
                  ),
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton<LeaveType>(
                      isExpanded: true,
                      //initial value 
                      value: selectedLeave != null ? items[0] : null,
                      icon: const Icon(Icons.arrow_downward),
                      iconSize: 24,
                      elevation: 16,
                      hint: text("Leave Type"),
                      style: const TextStyle(
                          color: Colors.black
                      ),
                      onChanged: (LeaveType  value) {
                        setCustomState(() {
                          selectedLeave = value;
                          items.remove(selectedLeave);
                          items.insert(0, selectedLeave);
                        });
                      },
                      items: items
                          .map((leave) {
                        return  new DropdownMenuItem<LeaveType>(
                          value: leave,
                          child: text(leave.name),
                        );
                      }).toList(),
                    ),
                  ),
                ),
              ),
            ]
        );
  }
Ilyas Arafath
  • 511
  • 7
  • 13
1

I changed as below and it got solved:

Initial Code:

List<GamesModel> users = <GamesModel>[
  new GamesModel(1,"Option1"),
  new GamesModel(2,"Option2"),

];
return users;

Changed Code:

List<GamesModel> users = <GamesModel>[
      const GamesModel(1,"Option1"),
      const GamesModel(2,"Option2"),
];
return users;

If anybody want i can put the whole code

ankurgoel
  • 41
  • 1
  • 8
1

Note that if the list has duplicated values, it will also has this error. For example, if languages = ["English", "English", "French"];

then if I set the default language = "English".

      DropdownButton<String>(
        value: language,
        icon: const Icon(Icons.arrow_downward),
        iconSize: 24,
        elevation: 16,
        style: const TextStyle(color: AppColors.highLightTextColor),
        underline: Container(
          height: 1,
          color: AppColors.underLineColor,
        ),
        onChanged: (String? newValue) async {
          setState(() {
            language = newValue;
          });
        },
        items: languages.map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),

Remove the duplicate values, then it works.

Liping Xiong
  • 316
  • 2
  • 8
1

In my case, i use empty String for default

value : dropdownValue != "" ? dropdownValue : null

Like this, errors be gone

0

you can avoid the null value using a ternary operator:

  Container(
             child:
              new DropdownButton<String>(
              value: dropdownValue ?? "1",
              icon: const Icon(Icons.arrow_downward),
              iconSize: 24,
              elevation: 16,
              style: const TextStyle(color: Colors.black, fontSize: 18),
              underline: Container(height: 2, color: Colors.white24, ),
              items: <String>['1', '2', '3', '5'].map((String value) {
              return new DropdownMenuItem<String>(
              value: value,
              child: new Text(value),
              );}).toList(),
              onChanged: (value) {
                  setState(() { dropdownValue=value;});
              },
          )),
Al Walid Ashik
  • 1,545
  • 20
  • 32
ALEXANDER LOZANO
  • 1,874
  • 3
  • 22
  • 31
0
          DropdownButton<String>(
            iconEnabledColor: Colors.cyan.withOpacity(.6),
            isExpanded: true,
            itemHeight: 50,
            iconSize: 30,
            hint: Text("Choose Province"),
            items: _provinces
                .map((e) => DropdownMenuItem(
              child: Text(e),
              value: e,
            ))
                .toList(),
            value: _Province,
            onChanged: (String? value) async{
              final respnose=await FirebaseFirestore.instance.collection('city').where('provinceName',isEqualTo: value).get();
              _city=[];
              for(var item in respnose.docs){
                print(item.data());
                _city.add(item.data()['name']);
              }
              
              print(_Province);
              setState(() {
                _city=_city;
                _Province = value;
              });
            },
          ),
          SizedBox(height: 20,),

          DropdownButton<String>(
            iconEnabledColor: Colors.cyan.withOpacity(.6),
            isExpanded: true,
            itemHeight: 50,
            iconSize: 30,
            hint: Text("Choose City"),
            items:_city
                .map((e) => DropdownMenuItem(
              child: Text(e),
              value: e,
            ))
                .toList(),
            value: _City,
            onChanged: (String? value) async{
              setState(() {
                _town=[];
                _Town=null;
              });
              print(_town);
              final respnose=await FirebaseFirestore.instance.collection('town').where('cityName',isEqualTo: value).get();
              print(respnose.docs);


              for(var item in respnose.docs){
                print(item.data());
                _town.add(item.data()['name']);
              }
              print(_town);
              print(_City);
              setState(() {

                _City = value;
                _town=_town;
              });
            },
          ),
          SizedBox(height: 20,),
          
          if(true)
          DropdownButton<String>(
            iconEnabledColor: Colors.cyan.withOpacity(.6),
            isExpanded: true,
            itemHeight: 50,
            iconSize: 30,
            hint: Text("Choose Town"),
            items:_town
                .map((e) => DropdownMenuItem(
              child: Text(e),
              value: e,
            )
            )
                .toList(),
            value: _Town,
            onChanged: (String? value)async {
              print(_Town);
              setState(() {
                _Town = value;
              });

         
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
  • 1
    Please provide some explanation and format your code. See [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – tomasantunes Jun 29 '21 at 09:46
0

This error also occurs if you forget to give dropdown menu items a value. ==== WORKS ====

<String>['A', 'B', 'C'].map<DropdownMenuItem<String>>((vehicle) {
        print("vehicle is $vehicle");
        print("vehicle is equal ${vehicle == x.value}");
        return DropdownMenuItem<String>(
          value: vehicle,
          child: Text(
            // vehicle.vehicleInfo!.vehicleType!,
            vehicle,
            style: TextStyle(
              color: Colors.grey[600],
            ),
          ),
        );
      }).toList(),

==== DOESNT WORK ====

<String>['A', 'B', 'C'].map<DropdownMenuItem<String>>((vehicle) {
        return DropdownMenuItem<String>(
          child: Text(
            vehicle,
            style: TextStyle(
              color: Colors.grey[600],
            ),
          ),
        );
      }).toList(),
Philip Kalela
  • 125
  • 13
0

I had the same problem, and the solution is to fill the value of DropdownButton(value: (use a value from the items you set) you can not use any value you want, but it should be one of the items that you set for the DropdownMenuItem.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 2
    There are thirteen existing answers to this question, including an accepted answer with 58 upvotes. Are you _sure_ your answer hasn't already been provided? If not, why might someone prefer your approach over the existing approaches proposed? Are you taking advantage of new capabilities? Are there scenarios where your approach is better suited? Also, did you mean "you _can_ use any value you want"? Otherwise, the statement doesn't make sense. – Jeremy Caney Nov 21 '21 at 22:03
0

I think because of the update in the framework, the error came out

Here is how you can solve it

DropdownButton(
              hint: const Text("Please choose your gender"),
              items: <String>["Male", "Female", "Rather not say"]
                  .map<DropdownMenuItem<String>>((e) {
                return DropdownMenuItem<String>(
                    value: e, child: Text(e.toString()));
              }).toList(),
              onChanged: (String? value) {
                setState(() {
                  dropdownValue = value!;
                });
              });

Note that: dropdownValue is a string variable defined at the top

0

If you are loading the list from an api that returns list, look at what i did to debug the error.

  1. Created a reusable widget that handle future response

     Widget rangeLists(selectedValue) {
     return FutureBuilder(
         future: YourFuture,//this should return Future<List>
         builder: (context, snapshot) {
           if (!snapshot.hasData) {
             return Text('Loading...');
           } else {
             List<DropdownMenuItem<String>> categoriesItems = [
               DropdownMenuItem(
                 child: Text(selectedValue),
                 value: selectedValue,
               ),
             ];
             print('categoriesItems.last.value');
             print(categoriesItems.last.value);
             var snapshotAsMap = snapshot.data as List;
             for (int i = 0; i < snapshotAsMap.length; i++) {
               if (snapshotAsMap[i]['category'] != selectedValue) {
                 categoriesItems.add(
                   DropdownMenuItem(
                     child: Text(snapshotAsMap[i]['category']),
                     value: snapshotAsMap[i]['category'],
                   ),
                 );
               }
             }
             return Padding(
               padding: const EdgeInsets.only(left: 18.0, right: 18, top: 10),
               child: Container(
                 padding: EdgeInsets.only(left: 25, right: 25),
                 decoration: BoxDecoration(
                     border: Border.all(color: Colors.grey, width: 1),
                     borderRadius: BorderRadius.circular(25)),
                 child: DropdownButton<String>(
                   items: categoriesItems,
                   icon: const Icon(
                     Icons.expand_more,
                     color: Colors.grey,
                   ),
                   iconSize: 24,
                   elevation: 16,
                   isExpanded: true,
                   style: const TextStyle(color: Colors.grey),
                   underline: SizedBox(),
                   onChanged: (value) {
                     setState(() {
                       widget.selectedValue = value;
                     });
                   },
                   value: selectedValue,
                   hint: Text('My courses'),
                 ),
               ),
             );
           }
         })};
    

2.Usage you can called it like this

String selectedValue="Select Here"

rangeLists(selectedValue)//call this as a widget in ur ui

It will handle all list from the Api backend when u return a list u don't need to worry about the error any more

Arinzehills
  • 1,491
  • 10
  • 14
0

enter image description here

I was searching a lot to find a better solution and at the and i found the solution if you clear the list at the end then it will not show the error for example your list name is "xyz" then at the end write xyz.clear() it will solve your problem 100%

-1
    child: DropdownButtonFormField<String>(
      hint: Text(widget.hintText == "Select value..."
          ? "Select ${widget.caption}"
          : widget.hintText),
      items: getItems(),
      value: **checkValue(widget.currentValue)**,
      iconSize: 30,
      onChanged: widget.onChanged,
    ),
    
    String? **checkValue(String? value)** {
      var arrRet = widget.items.where(
          (item) => item[widget.valueMember].toString() == value.toString());
      if (arrRet.isEmpty && widget.items.isNotEmpty)
        return widget.items[0][widget.valueMember].toString();
      return value;
    }
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – MaLa Dec 29 '22 at 21:02