如何在具有空安全性的颤振中创建构造函数

时间:2021-03-15 13:51:55

标签: flutter dart

这对我来说有点棘手。因为我们不能再在构造函数中传递空值我有这个

class LeadingButton extends StatelessWidget {
  final IconData icon;
  final Color color;
  final Color iconColor;
  final double iconSize;
  final double size;
  final Color backGroundColor;
  final VoidCallback onPressed;
  final double padding;
  final EdgeInsets margin;
  final double rotate;

  LeadingButton({
      @required this.icon,
      this.color = Colors.transparent,
      @required this.onPressed,
      this.size = 20,
      this.backGroundColor = Colors.transparent,
      this.iconSize = 20,
      this.iconColor = darkColor,
      this.rotate = 0,
      this.padding = 0,
      this.margin = const EdgeInsets.all(10)});
...

我在 icononpressed 函数上遇到错误。虽然 文档说

<块引用>

参数“onPressed”和“icon”不能有值 'null' 因为它的类型,但隐含的默认值是 '无效的'。尝试添加一个明确的非“空”默认值或 “必需”修饰符。

我想我错过了一些东西,请介意分享

2 个答案:

答案 0 :(得分:1)

不确定,但这可能是由于最近更改了关键字所致。 您是否尝试使用 required 关键字?

https://dart.dev/null-safety/faq#how-does-required-compare-to-the-new-required-keyword

答案 1 :(得分:0)

我认为 LeadingButtononPressed 类型需要更改为 VoidCallback? 以便它可以接受空值。

相关问题