gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:02:17 24 4
gpt4 key购买 nike

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

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

Future _state;
Future _city;

@override
void initState() {
super.initState();
_state = _fetchStates();
}
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');
}
}
  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');
}
}

状态下拉列表

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,
);
}),

城市下拉列表

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,
);
}),
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'];
}
}
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'];
}

最佳答案

您必须在 State 下拉列表的 onChanged 回调中设置 cityModel = null

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

此处发生此错误,因为您传递的不在DropdownButtonFormField(城市下拉菜单)的items中。

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

您还可以引用这个例子:DartPad

关于android - 如何在flutter中制作依赖的多级DropDown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60433200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com