Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
                                        });
                                      },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading