當我在 Dropdown Flutter 中使用 Hospital 作為 onChangeEvent 的引數時,會出現以下錯誤:
引數型別“void Function(Hospital)”不能分配給引數型別“void Function(Object?)?”。
醫院
class Hospital {
final int Id;
final String Name;
final int BranchId;
const Hospital(
{required this.Id, required this.Name, required this.BranchId});
factory Hospital.fromJson(Map<String, dynamic> json) {
return Hospital(
Id: json['id'], Name: json['name'], BranchId: json['branchId']);
}
}
下拉代碼
DropdownButton(
style: const TextStyle(
color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Hospital newValue) {
setState(() {
hospitalDropdownValue = newValue.id;
branchDropdownValue = newValue.BranchId;
});
},
items: _hospitals.map<DropdownMenuItem<Hospital>>((Hospital item) {
return DropdownMenuItem<Hospital>(
child: Text(item.Name),
value: item,
);
}).toList(),
value: hospitalDropdownValue,
hint: const Text("Hospital Name"),
),
uj5u.com熱心網友回復:
您可以嘗試分配DropdownButton型別Hospital以及
DropdownButton<Hospital>(
style: const TextStyle(
color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (Hospital? newValue){//add null safe
setState(() {
hospitalDropdownValue = newValue.id;
branchDropdownValue = newValue.BranchId;
});
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449679.html
