【发布时间】:2021-08-06 11:57:34
【问题描述】:
我有这个类,尝试插入图标并可能根据rotationAngle 在容器内旋转它。
class ReusableCardIconLayout extends StatelessWidget {
final double? rotationAngle;
ReusableCardIconLayout({this.rotationAngle});
@override
Widget build(BuildContext context) {
return Expanded(
flex: 2,
child: Transform.rotate(
angle: rotationAngle == null ? 0.0 : rotationAngle,
),
);
}
}
其中,rotationAngle 是可选的,因此我将其设为可为空。
现在,一行
angle: rotationAngle == null? 0.0 : rotationAngle,
显示错误“参数类型'double?'不能分配给参数类型'double'"。
为什么会出现这样的错误?我已经检查过它是否为空。如果是,则给出默认值 0.0 或使用它的值。这行有什么问题?
有没有办法像上面那样使用三元运算符来解决呢?
是否只给了默认参数值剩下的选项?
【问题讨论】: