【问题标题】:The argument type 'void Function()? Function(int)' can't be assigned to the parameter type 'void Function(int?)?' after null safety参数类型 'void Function()? Function(int)' 不能分配给参数类型 'void Function(int?)?'在零安全之后
【发布时间】:2021-11-05 08:52:04
【问题描述】:

我想将我的 Flutter 应用迁移到 null 安全性,但我遇到了这个错误

参数类型'void Function()? Function(int)' 不能分配给参数类型 'void Function(int?)?'在“OnTap:handleItemSelected,

_NavigationBarItem(
            index: 0,
            lable: 'Messages',
            icon: CupertinoIcons.bubble_left_bubble_right_fill,
            isSelected: (selectedIndex == 0),
            onTap: handleItemSelected,
          ),

这是我的 HandleItemSelected 无效

 void handleItemSelected(int index) {
    setState(() {
      selectedIndex = index;
    });
    widget.onItemSelected!(index);
  }

有人知道为什么吗?

【问题讨论】:

  • 您的索引参数应该是一个可以为空的整数,即 void handleItemSelected(int? index) {...}

标签: flutter dart dart-null-safety


【解决方案1】:

像这样更新你的代码:

    Radio<int>(
       value: 0,
       groupValue: gvalue,
       onChanged: handleChange,
   ),



void handleChange(int? value) {
    setState(() {
      gvalue = value!;
      print(gvalue);
    });

【讨论】:

    猜你喜欢
    • 2021-02-05
    • 2022-10-31
    • 2023-04-05
    • 1970-01-01
    • 2021-10-22
    • 2022-08-05
    • 2021-02-12
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多