Fle*_*exl 5 dart flutter dart-null-safety
升级到 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()?'.
我已经在寻找答案和可能的解决方案很长时间了,但我还没有找到。任何帮助将非常感激。
您正在将Function()
值传递给void Function()
参数,就像它所说的那样。更改您的声明,final void Function() onPressed;
以便键入更接近匹配,并且不可能返回 null 或接受参数。
归档时间: |
|
查看次数: |
3325 次 |
最近记录: |