Home > OS >  The argument type 'void Function(Hospital)' can't be assigned to the parameter type &
The argument type 'void Function(Hospital)' can't be assigned to the parameter type &

Time:03-25

when I use Hospital as a parameter of onChangeEvent in Dropdown Flutter gives this erroe :

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

Hospital

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

Dropdown Code

                                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"),
                                    ),

CodePudding user response:

You can try to assign DropdownButton type as Hospital as well as

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