升级到 Flutter 1.25.0-8.1.pre 后默认开启空安全,我开始修改我的项目代码。除了作为参数传递的函数外,一切正常,如下例所示:
class AppBarIcon extends StatelessWidget {
final IconData icon;
final Function onPressed;
const AppBarIcon({Key? key, required this.icon, required this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return CupertinoButton(
child: Icon(icon, size: 28, color: Colors.white),
onPressed: onPressed,
);
}
}
Run Code Online (Sandbox Code Playgroud)
onPressed是必需参数,因此它不能为空,但是在尝试将函数传递给CupertinoButton时出现错误:
The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.
我已经在寻找答案和可能的解决方案很长时间了,但我还没有找到。任何帮助将非常感激。