【问题标题】:How best to convert old Dart code that triggers "parameter can't be 'null' but the implicit default value is 'null'." error?如何最好地转换触发“参数不能为'null'但隐式默认值为'null'”的旧Dart代码。错误?
【发布时间】:2021-12-22 15:28:15
【问题描述】:

采用以下非空安全 Dart 代码:

class RoundedButton extends StatelessWidget {
  RoundedButton({this.title, this.color, @required this.onPressed});

  final Color color;
  final String title;
  final Function onPressed;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        elevation: 5.0,
        color: color,
        borderRadius: BorderRadius.circular(30.0),
        child: MaterialButton(
          onPressed: onPressed,
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            title,
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}

关于构造函数的参数,Android Studio 是说,“参数 [parameter] 因为它的类型不能有 'null' 的值,但隐含的默认值是 'null'。”

我理解错误并发现我可以像这样简单地修改代码:

class RoundedButton extends StatelessWidget {
  RoundedButton({this.title, this.color, required this.onPressed});

  final Color? color;
  final String? title;
  final Function onPressed;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        elevation: 5.0,
        color: color,
        borderRadius: BorderRadius.circular(30.0),
        child: MaterialButton(
          onPressed: onPressed.call(),
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            title!,
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}

这样,我满足了语言规则,但使用 bang 运算符进行“强制解包”可能是不受欢迎的。

所以我的解决方案看起来很老套......所以在这种情况下,将这段代码转换为空安全的优雅、适当甚至性感的方法是什么,如果有多种方法,那么优缺点是什么?

谢谢!

【问题讨论】:

  • 您应该在空安全代码中使用required 关键字,而不是旧的@required 注释。如果您要求不可为空的命名参数,则默认情况下它们不会为空。
  • 问题是有效的。如果我们不想每次都使用构造函数中的所有值,如果需要所有值,我们如何处理这些事情。

标签: flutter dart dart-null-safety null-safety


【解决方案1】:

有几种方法可以迁移到 null 安全代码,正如您所说,使所有变量都可以为 null 是一种可能的解决方案,这里有另外两种:

1。使变量具有默认值

来自:

final String title;

到:

final String title = '';

MyClass {
  MyClass({this.title=''});
  final String title;
}

何时使用

如果您有一个变量应该以某个值开头并随着时间的推移而变化,或者如果您有一个变量不太可能对每个实例都是唯一的。

int timesOpened = 0;

不好

int uniqueValue = 0 // if it should be unique, it shouldn't be default.

2。在构造函数处强制一个值

来自:

MyClass {
  MyClass({this.title});
  final String title;
}

到:

MyClass {
  MyClass({required this.title});
  final String title;
}

何时使用

当您有一个必须传递给每个实例的值并且每​​个实例可能不同时

MyClass({required this.onPressed});

不好

MyClass({required this.textSize}); // probably should have a default text size value

如果我是你,我会让 color 有一个默认值,并且 title 和 onPressed 是必需的参数。但你会比我更清楚。

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 2021-12-22
    • 2022-06-14
    • 2021-06-24
    • 2021-10-28
    • 2021-11-12
    • 2022-01-03
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多