0
votes

I have 2 dropdowns. when selected item from first dropdown. I need add items to second dropdown but it giving error.

I checked the second dropdown's items is already empty. when first dropdown changed second's dropdown's items is not empty but doesn't work and gived error.

Any idea?

first dropdown code:

Widget getPolWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Icon(
          Icons.touch_app,
          size: 25.0,
          color: Colors.black,
        ),
        SizedBox(width: 50.0),
        DropdownButton(
          items: this.polList
              .map((value) => DropdownMenuItem(
                    child: Text(
                      value.code,
                      style: TextStyle(color: Colors.black),
                    ),
                    value: value.id,
                  ))
              .toList(),
          onChanged: (selectedType) {

          getPod(selectedType);

            setState(() {
              selectedPodId = selectedType;
            });
          },
          value: selectedPodId,
          isExpanded: false,
          hint: Text(
            'Choose Type',
            style: TextStyle(color: Colors.black),
          ),
        )
      ],
    );
  }



Widget getPodWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Icon(
          Icons.touch_app,
          size: 25.0,
          color: Colors.black,
        ),
        SizedBox(width: 50.0),
        DropdownButton(
          items: this.podList
              .map((value) => DropdownMenuItem(                
                    child: Text(
                      value.code,
                      style: TextStyle(color: Colors.black),
                    ),
                    value: value.id,
                  ))
              .toList(),
          onChanged: (selectedType) {
            setState(() {
              selectedPodId = selectedType;
              print(selectedPodId);
            });
          },
          value: selectedPodId,
          isExpanded: false,
          hint: Text(
            'Choose Type',
            style: TextStyle(color: Colors.black),
          ),
        )
      ],
    );
  }

Error message:'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.

1
There are 2 solution: 1- Maybe consider adding a flag that hides the second one until the first one is selected. Something like this ``` return selectedPodId == null? Container():Row(... 2- Flutter doesn't allow to not have a value when creating the widget, so u can set a default value for selectedPodIdKarim

1 Answers

0
votes

Hello man You have to declare one variable and pass into the value parameter in DropdownButton and In the onchanged method you have to update that variable

String a = "fdhsa";
value:a,
onchanged:(h){setState(){a=h;}},

Make sure that your value parameter same from the that list which is inherit in dropdownbutton