0
votes

In my code I added a dropdown which looks like below, When I switched the selection in dropdown its not getting updated its showing exception, I have declared a variable in statefull widget , In my dropdown function I am assigning that as A value to the dropdown Button, and in Onchanged I am passing the json to another function there I am taking the value from a variable and assigning it to a opSelected variable inside the setState

class _ReportFilterState extends State<ReportFilter> {

  String opSelected;
 //declared string to hold the selected value in dropdown within the state class.

    buildMainDropdown(List<Map<String, Object>> items, StateSetter setState) {
        return Container(
          child: Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 27.0,
              vertical: 16.0,
            ),
            child: Align(
              alignment: Alignment.topLeft,
              child: DropdownButtonHideUnderline(
                child: DropdownButton(
                  isExpanded: true,
                  hint: Text("Choose Filters"),
                  value: opSelected, // Here assigning the value 
                  items: items
                      .map((json) => DropdownMenuItem(
                          child: Text(json["displayName"]), value: json))
                      .toList(),
                  onChanged: (json) {
                    manageIntState(json, setState);
                  },
                ),
              ),
            ),
          ),
        );
      }

 void manageIntState(Map<String, Object> jsonSelected, StateSetter setState) {
    setState(() {
      dispName = jsonSelected["displayName"]; 

//here I am setting the selected value
      opSelected = dispName;

//Doing some operations
      id = jsonSelected['id'];
      type = jsonSelected['type'];
      selectedFilterOption = jsonSelected;

      if (jsonSelected.containsKey("data")) {
        List<Map<String, Object>> tempList;
        List<String> dailogContent = List<String>();
        tempList = jsonSelected['data'];

        tempList
            .map((val) => {
                  dailogContent.add(val['displayId']),
                })
            .toList();
        _showReportDialog(dailogContent);
      }
    });
  }

But when I run I will end up with error

items==null|| items.isEmpty||value==null||itsems.where((DropdownMenuItem item)=>item.value==value).length==1 is not true ..

Let me know what I have done wrong in code so its giving me like this, if I commented its not showing the selected dropdown value.

1

1 Answers

0
votes

That error happens when the selected value of the DropdownButton is not one of the values of it's items.

In your case, your items values are json which is a Map<String, Object>, and the value of the DropdownButton is opSelected which is a String.

So you need to change the type of opSelected like this:

Map<String, Object> opSelected;

Also make sure you are passing a reference of the same list of items to buildMainDropdown(), because if you are creating a new list while calling buildMainDropdown() then the DropdownButton will have another reference of options and it's not allowed


Note: you may want yo use dynamic instead of Object for the Map, like this:

Map<String, dynamic> opSelected;

Here is why: What is the difference between dynamic and Object in dart?