BoxPainter createBoxPainter([onChanged]) => 参数 onChanged 的值不能为 null
BoxPainter createBoxPainter([onChanged]) => The parameter onChanged can't have a value of null
我有 copied/pasted 一些用于创建自定义 BoxDecoration 的示例代码:
class FrameDecoration extends Decoration {
@override
BoxPainter createBoxPainter([onChanged]) {
return _CustomDecorationPainter();
}
我收到这个错误:
The parameter 'onChanged' can't have a value of 'null' because of its
type, but the implicit default value is 'null'.
Try adding either an explicit non-null default value or the 'required'
modifier.
好的,这是有道理的:这是一个无效的安全问题。我的pubspec.yaml“环境”:sdk: ">=2.12.0 <3.0.0"
所以我尝试添加“必需”:
class FrameDecoration extends Decoration {
@override
BoxPainter createBoxPainter(required [onChanged]) {
return _CustomDecorationPainter();
}
这次错误是:
'FrameDecoration.createBoxPainter' ('BoxPainter Function(void
Function())'] isn't a valid override of 'Decoration.createBoxPainter'
('BoxPainter Function('void Function()])').
我尝试了其他几种方法 - 不开心。
两条消息还说:
The onChanged argument ... can be omitted if there is no change that
the painter will change.
我试过“无参数”(“createBoxPainter()”),我试过一个空列表(“createBoxPainter([])”)。还是不开心。
我只想用自定义的“paint()”方法创建自己的“装饰”class。
问:从 createBoxPainter() 中省略 onChanged 的正确语法是什么?
问:本例中“createBoxPainter()”的推荐语法是什么?
pedro pimont 给了我我正在寻找的语法:
@override
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
return _CustomDecorationPainter();
}
// <= Explicitly adding the type, and making it nullable, resolved the compile error
您尝试覆盖的 Decoration
class 中的 createBoxPainter
需要一个可选的 VoidCallback onChanged
参数,因此如果您不为其提供默认值,您还必须使用 ?
将其标记为 nullable,试试这个:
BoxPainter createBoxPainter([VoidCallback? onChanged])
另外,尽管下面的 none 可以工作,但关于 Dart 语法,您将根据需要标记一个可选参数,这是不允许的。
使用命名参数使用所需的关键字使用 {}
:
BoxPainter createBoxPainter({required Function onChanged})
或通过删除 []
使其成为必需
BoxPainter createBoxPainter(Function onChanged)
我有 copied/pasted 一些用于创建自定义 BoxDecoration 的示例代码:
class FrameDecoration extends Decoration {
@override
BoxPainter createBoxPainter([onChanged]) {
return _CustomDecorationPainter();
}
我收到这个错误:
The parameter 'onChanged' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-null default value or the 'required' modifier.
好的,这是有道理的:这是一个无效的安全问题。我的pubspec.yaml“环境”:sdk: ">=2.12.0 <3.0.0"
所以我尝试添加“必需”:
class FrameDecoration extends Decoration {
@override
BoxPainter createBoxPainter(required [onChanged]) {
return _CustomDecorationPainter();
}
这次错误是:
'FrameDecoration.createBoxPainter' ('BoxPainter Function(void Function())'] isn't a valid override of 'Decoration.createBoxPainter' ('BoxPainter Function('void Function()])').
我尝试了其他几种方法 - 不开心。
两条消息还说:
The onChanged argument ... can be omitted if there is no change that the painter will change.
我试过“无参数”(“createBoxPainter()”),我试过一个空列表(“createBoxPainter([])”)。还是不开心。
我只想用自定义的“paint()”方法创建自己的“装饰”class。
问:从 createBoxPainter() 中省略 onChanged 的正确语法是什么?
问:本例中“createBoxPainter()”的推荐语法是什么?
pedro pimont 给了我我正在寻找的语法:
@override
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
return _CustomDecorationPainter();
}
// <= Explicitly adding the type, and making it nullable, resolved the compile error
您尝试覆盖的 Decoration
class 中的 createBoxPainter
需要一个可选的 VoidCallback onChanged
参数,因此如果您不为其提供默认值,您还必须使用 ?
将其标记为 nullable,试试这个:
BoxPainter createBoxPainter([VoidCallback? onChanged])
另外,尽管下面的 none 可以工作,但关于 Dart 语法,您将根据需要标记一个可选参数,这是不允许的。
使用命名参数使用所需的关键字使用 {}
:
BoxPainter createBoxPainter({required Function onChanged})
或通过删除 []
BoxPainter createBoxPainter(Function onChanged)