当我使用医院作为下拉菜单中onChangeEvent的参数时,Flutter 会产生以下错误:

无法将参数类型"void Function(医院)"分配给

医院

class 医院 {
  final int Id;
  final String Name;
  final int BranchId;
  const 医院(
      {required this.Id, required this.Name, required this.BranchId});

  factory 医院.fromJson(Map<String, dynamic> json) {
    return 医院(
        Id: json['id'], Name: json['name'], BranchId: json['branchId']);
  }
}

下拉代码

                                DropdownButton(
                                      style: const TextStyle(
                                          color: Colors.deepPurple),
                                      underline: Container(
                                        height: 2,
                                        color: Colors.deepPurpleAccent,
                                      ),
                                      onChanged: (医院 newValue) {
                                        setState(() {
                                          hospitalDropdownValue = newValue.id;
                                         branchDropdownValue = newValue.BranchId;
                                        });
                                      },
                                      items: _hospitals.map<DropdownMenuItem<医院>>((医院 item) {
                                        return DropdownMenuItem<医院>(
                                          child: Text(item.Name),
                                          value: item,
                                        );
                                      }).toList(),
                                      value: hospitalDropdownValue,
                                      hint: const Text("医院 Name"),
                                    ),

推荐答案

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

Flutter相关问答推荐

获取屏幕大小的滚动视图与动态大小?

Flutter 蜂窝-检索非基元数据类型列表

UI中的Flutter 下拉列表值未更改

我有相关的下拉菜单Flutter Windows应用程序的问题

Ffmpeg_kit_fltter:^6.0.3版权问题

如何管理枚举类型的阻塞事件?

如何在Flutter 中创造以下效果

在Ffltter中,我已经使用了Ffltter_BLOC Cupit和Pusher,但当获得新数据时,UI没有更新

flatter_localizations错误在Null值上使用了Null判断运算符

Flutter 附近的连接

Flutter 中的 setState() 内部是如何工作的?

尽管我的 url 链接正确,但 ArgumentError(无效参数:URI 文件中未指定主机:///)错误

有没有办法在 google_map 上移动标记(向左或向右移动一些像素)?

如何从列表中更新provider变量:Flutter列表和Provider优化指南

如何将此文本按钮剪辑在堆栈中的圆形头像上方?

如何更改 DropDownMenu 的显示方向?

在 Flutter 中读取 Firebase 数据库数据时遇到问题

是否可以在 Flutter 中使用 ToggleButtons 在页面 PageView 之间切换?

为什么这个参数类型不能分配给参数类型?

如何在 ElevatedButton 上设置背景 colored颜色 动画?