相关疑难解决方法(0)

无法将参数类型“Function”分配给参数类型“void Function()?” 空安全后

我想实现制作一个带有不同项目的抽屉,所以我正在为 DrawerItems 创建一个单独的文件,并使用构造函数将数据传递给主文件。但是我在“onPressed”函数上收到以下错误:

“不能将参数类型‘Function’分配给参数类型‘void Function()’”

class DrawerItem extends StatelessWidget {
    
      final String text;
      final Function onPressed;
    
      const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FlatButton(
          child: Text(
            text,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 18.0,
            ),
          ),
          onPressed: onPressed,
        );
      }
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?

dart flutter dart-null-safety

35
推荐指数
6
解决办法
2万
查看次数

标签 统计

dart ×1

dart-null-safety ×1

flutter ×1