2

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

1 Answer 1

2

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;
                                        });
                                      },
1
  • Thanks , It works. after adding type <Hospital> and onChange: (Hospital? newValue) { }
    – MD SHAFFAT
    Mar 25, 2022 at 5:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.