【问题标题】:how to create a constructor in flutter with null safety如何在具有空安全性的颤动中创建构造函数
【发布时间】:2021-06-12 19:06:12
【问题描述】:

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

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' 因为它的类型,但隐含的默认值是 '空值'。尝试添加显式的非“null”默认值或 “必需”修饰符。

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它的类型是VoidCallBack 也是required modifier 的意思不是在前面放一个@required
      • 是您自己的LeadingButton 类吗?还是来自你无法控制的 lib?
      • 它是我的,上面是它的构造函数
      猜你喜欢
      • 2020-10-14
      • 2021-12-08
      • 2021-03-30
      • 2023-03-05
      • 2021-08-12
      • 2021-06-26
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多