【问题标题】:Is there "!." operator in dart (flutter)?有没有 ”!。”飞镖中的运算符(颤振)?
【发布时间】:2021-02-27 19:44:24
【问题描述】:

Flutter 源文件多次包含类似这样的代码:

 @override
  double computeMinIntrinsicWidth(double height) {
    if (child != null)
      return child!.getMinIntrinsicWidth(height);
    return 0.0;
  }

请解释一下“!”。我在飞镖操作员列表中找不到它。

【问题讨论】:

  • 可能类似于“空断言”运算符?
  • 有条件成员访问运算符“?.”在飞镖中,但我问的是“!。”。
  • 好的。我想也许代码中的某个地方有重载运算符“!”,所以 child!.getMinIntrinsicWidth(height) 实际上是 child.operator!().getMinIntrinsicWidth(height) ...
  • 相关的 SO 问题? stackoverflow.com/questions/60068435

标签: flutter dart


【解决方案1】:

后缀感叹号 (!) 采用左侧的表达式并将其转换为其底层的不可为空的类型。所以它改变了:

String toString() {
  if (code == 200) return 'OK';
  return 'ERROR $code ${(error as String).toUpperCase()}';
}

到这样的事情:

String toString() {
  if (code == 200) return 'OK';
  return 'ERROR $code ${error!.toUpperCase()}';
}

您可以在this document 中阅读有关 null 安全性的更多信息。

【讨论】:

    【解决方案2】:

    它是“(not)null assertion operator”,它将成为 Dart 的一部分,在下一个版本中具有 Null Safety 功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2022-07-20
      • 2021-07-10
      • 2021-11-18
      • 2020-09-20
      相关资源
      最近更新 更多