[英]How can I get the id of a selected item in a list using the the dropdownSearch widget in flutter?
I am using the DropDownSearch widget in flutter to display a list of items.我正在使用 flutter 中的 DropDownSearch 小部件来显示项目列表。 I am displaying the value "acYear" from the list but I want to be able to pass the value "id" of the selected item to the controller ie academicYearController.text.How can I achieve this please?
我正在显示列表中的值“acYear”,但我希望能够将所选项目的值“id”传递给 controller,即 academicYearController.text。请问我该如何实现? This is what I have tried:
这是我试过的:
child: DropdownSearch < dynamic > ( autoValidateMode: AutovalidateMode.onUserInteraction, validator: (value) => value == null? "Academic Year is required ": null, dropdownDecoratorProps: const DropDownDecoratorProps( dropdownSearchDecoration: InputDecoration( hintText: "Select academic year", helperText: '', labelText: "Academic Year *", contentPadding: EdgeInsets.fromLTRB(12, 12, 0, 0), border: OutlineInputBorder(), ), ), items: dataService.academicYearList.map((AcademicYear yr) { return yr.accYear.toString(); }).toList(), // items: dataService.academicYearList.map((AcademicYear yr) { // return DropdownMenuItem( // child: Text(yr.id.toString()), // value: yr.id, // ); // }).toList(), onChanged: (value) { academicYearController.text = value; setState(() { dataService.selectedAcademicYear = dataService.selected; }); debugPrint('selectedId: ${academicYearController.text}'); }, ),
And the AcademicYear Object and academicYear List look like this: AcademicYear Object 和 academicYear List 如下所示:
class AcademicYear { int id; String accYearId; String accYear; int status; AcademicYear({ required this.id, required this.accYearId, required this.accYear, required this.status, }); [{ "id": 1, "accYearId": "20212022", "accYear": "20212022", "status": 1 }]
I have Added the API call to get the academicYear data just incase am doing something wrong as shown:我已经添加了 API 调用来获取 academicYear 数据,以防我做错事,如下所示:
Future < List < AcademicYear >> getYears() async { academicUrl = "${dataService.baseUrl}/academicyear"; // debugPrint('url: $academicUrl'); http.Response response = await http.get( Uri.parse(academicUrl), headers: { "Content-Type": "application/json", }, ); List < AcademicYear > result(dynamic str) => List < AcademicYear >.from( json.decode(str).map((x) => AcademicYear.fromJson(x))); // debugPrint('result: ${response.body}'); if (response.statusCode == 200) { return result(response.body); } else { throw Exception('error loading object'); } }
In your DropdownSearch's
onChanged, do this:在您的
DropdownSearch's
onChanged 中,执行以下操作:
onChanged: (value) {
var xList = dataService.academicYearList.where((element) => element.accYear == value);
if (xList.isNotEmpty) {
AcademicYear x = xList.first;
print("x = ${x.id}");
academicYearController.text = value;
}
setState(() {
dataService.selectedAcademicYear = dataService.selected;
});
debugPrint('selectedId: ${academicYearController.text}');
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.