How to make dependent multilevel DropDown in flutter?

后端 未结 2 1849
忘掉有多难
忘掉有多难 2020-12-18 09:02

I am trying to make dependent multilevel dropdown first contains states list and second contains cities list, all the data fetched from API. Initially, I load state dropdown

2条回答
  •  星月不相逢
    2020-12-18 09:36

    You have to make cityModel = null in onChanged callback of State dropdown.

    setState(() {
      cityModel = null;
      stateModel = selectedState;
      _city = _fetchCities(stateModel.billstateid);
    });
    

    There should be exactly one item with [DropdownButton]'s value: Instance of 'City'. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

    This error occurs here, because the value you are passing not in the items of DropdownButtonFormField(city dropdown).

    When you select a State, you are fetching new list of city list and passing it to CityDropDown but forgot to clear the previously selected city(cityModel).

    You can also refer this example: DartPad

提交回复
热议问题