【发布时间】:2020-06-10 17:32:20
【问题描述】:
我正在实现从 API 填充的下拉菜单。数据已成功填充到下拉列表中,但是当我选择任何项目时出现此错误。各位大神可以帮帮我吗?
错误:应该只有一项具有 [DropdownButton] 的值:“Make”的实例。 检测到 0 个或 2 个或多个具有相同值的 [DropdownMenuItem]。
FutureBuilder<List<Make>>(
future: _fetchBrand(),
builder: (BuildContext context,
AsyncSnapshot<List<Make>> snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return DropdownButtonFormField<Make>(
isDense: true,
decoration: spinnerDecoration('Select Car Brand'),
items: snapshot.data
.map((user) => DropdownMenuItem<Make>(
child: Text(user.make),
value: user,
))
.toList(),
onChanged: (Make newVal) {
setState(() {
makeModel = newVal;
});
},
value: makeModel,
);
}),
Future<List<Make>> _fetchBrand() async {
var response = await http.get(url);
if (response.statusCode == 200) {
final items = json.decode(response.body).cast<Map<String, dynamic>>();
print(items);
List<Make> listOfUsers = items.map<Make>((json) {
return Make.fromJson(json);
}).toList();
return listOfUsers;
} else {
throw Exception('Failed to load internet');
}
}
class Make {
String makeid;
String make;
Make(
{this.makeid,
this.make,});
Make.fromJson(Map<String, dynamic> json) {
makeid = json['makeid'];
make = json['make'];
}
}
【问题讨论】:
-
你能分享你的示例json吗?