无法将参数类型 'void Function(Hospital)' 分配给参数类型 'void Function(Object?)?'。颤振下拉

The argument type 'void Function(Hospital)' can't be assigned to the parameter type 'void Function(Object?)?'. Flutter Dropdown

当我在 Dropdown Flutter 中使用 Hospital 作为 onChangeEvent 的参数时出现此错误:

The argument type 'void Function(Hospital)' can't be assigned to the parameter type '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"),
                                    ),

您可以尝试将 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;
                                        });
                                      },