如何在flutter中制作依赖的多级DropDown?

Jor*_*den 5 android ios dart flutter

我试图使依赖的多级下拉列表首先包含州列表,第二个包含城市列表,所有数据都是从 API 获取的。最初,我加载状态下拉列表,当我选择状态时,然后加载该状态的城市,如果我选择城市,则城市选择成功,但是当我更改状态值时会发生错误。如果在第一个下拉列表中进行更改,重新加载第二个下拉列表的正确方法是什么?

错误:应该只有一项具有 [DropdownButton] 的值:“城市”的实例。检测到零个或 2 个或更多 [DropdownMenuItem] 具有相同的值

Future _state;
Future _city;

@override
  void initState() {
    super.initState();
    _state = _fetchStates();
  }
Run Code Online (Sandbox Code Playgroud)
Future<List<StateModel>> _fetchStates() async {
    final String stateApi = "https://dummyurl/state.php";
    var response = await http.get(stateApi);

    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();

      List<StateModel> listOfUsers = items.map<StateModel>((json) {
        return StateModel.fromJson(json);
      }).toList();

      return listOfUsers;
    } else {
      throw Exception('Failed to load internet');
    }
  }
Run Code Online (Sandbox Code Playgroud)
  Future<List<City>> _fetchCities(String id) async {
    final String cityApi = "https://dummyurl/city.php?stateid=$id";
    var response = await http.get(cityApi);

    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();
      print(items);
      List<City> listOfUsers = items.map<City>((json) {
        return City.fromJson(json);
      }).toList();

      return listOfUsers;
    } else {
      throw Exception('Failed to load internet');
    }
  }
Run Code Online (Sandbox Code Playgroud)

状态下拉菜单

FutureBuilder<List<StateModel>>(
                                        future: _state,
                                        builder: (BuildContext context,
                                            AsyncSnapshot<List<StateModel>> snapshot) {
                                          if (!snapshot.hasData)
                                            return CupertinoActivityIndicator(animating: true,);
                                          return DropdownButtonFormField<StateModel>(
                                            isDense: true,
                                            decoration: spinnerDecoration('Select your State'),
                                            items: snapshot.data
                                                .map((countyState) => DropdownMenuItem<StateModel>(
                                              child: Text(countyState.billstate),
                                              value: countyState,
                                            ))
                                                .toList(),
                                            onChanged:(StateModel selectedState) {
                                              setState(() {
                                                stateModel = selectedState;
                                                _city = _fetchCities(stateModel.billstateid);
                                              });
                                            },
                                            value: stateModel,
                                          );
                                        }),
Run Code Online (Sandbox Code Playgroud)

城市下拉菜单

FutureBuilder<List<City>>(
                                        future: _city,
                                        builder: (BuildContext context,
                                            AsyncSnapshot<List<City>> snapshot) {
                                          if (!snapshot.hasData)
                                            return CupertinoActivityIndicator(animating: true,);
                                          return DropdownButtonFormField<City>(
                                            isDense: true,
                                            decoration: spinnerDecoration('Select your City'),
                                            items: snapshot.data
                                                .map((countyState) => DropdownMenuItem<City>(
                                              child: Text(countyState.billcity)
                                                .toList(),
                                            onChanged: (City selectedValue) {
                                              setState(() {
                                                cityModel = selectedValue;
                                              });
                                            },
                                            value: cityModel,
                                          );
                                        }),
Run Code Online (Sandbox Code Playgroud)
class StateModel {
  String billstateid;
  String billstate;
  String billcountryid;

  StateModel({this.billstateid, this.billstate, this.billcountryid});

  StateModel.fromJson(Map<String, dynamic> json) {
    billstateid = json['billstateid'];
    billstate = json['billstate'];
    billcountryid = json['billcountryid'];
  }
}
Run Code Online (Sandbox Code Playgroud)
class City {
  String billcityid;
  String billcity;
  String billstateid;

  City({this.billcityid, this.billcity, this.billstateid});

  City.fromJson(Map<String, dynamic> json) {
    billcityid = json['billcityid'];
    billcity = json['billcity'];
    billstateid = json['billstateid'];
  }
Run Code Online (Sandbox Code Playgroud)

Cra*_*Cat 8

您必须cityModel = nullonChanged 状态下拉菜单的回调中进行。

setState(() {
  cityModel = null;
  stateModel = selectedState;
  _city = _fetchCities(stateModel.billstateid);
});
Run Code Online (Sandbox Code Playgroud)

应该只有一项具有 [DropdownButton] 的值:“城市”的实例。检测到零个或 2 个或更多 [DropdownMenuItem] 具有相同的值

此处发生此错误,因为value您传递的对象不在itemsof DropdownButtonFormField(城市下拉列表)中。

当您选择一个州时,您正在获取城市列表的新列表并将其传递给 CityDropDown 但忘记清除先前选择的城市(cityModel)。

你也可以参考这个例子:DartPad