最近我一直在用Ffltter开发移动应用程序,当我查看TickerProvider的源代码时,我看到以下几行:

mixin SingleTickerProviderStateMixin<T extends StatefulWidget> on State<T> implements TickerProvider {
Ticker? _ticker;

  @override
  Ticker createTicker(TickerCallback onTick) {
    ...
    _ticker = Ticker(onTick, debugLabel: kDebugMode ? 'created by $this' : null);
    return _ticker!;
  }

...
}

我对这条线路很感兴趣:

return _ticker!;

我见过前面带有感叹号的布尔标识符,这意味着它将返回与其相反的值,但我从未见过这个.有人能告诉我这是干什么的吗?

推荐答案

这是DART拥有的零安全性的一部分.

你可以读到关于它的here

If you’re sure that an expression with a nullable type isn’t null, you can add ! to make Dart treat it as non-nullable

示例:

int? aNullableInt = 2;
int value = aNullableInt!; // `aNullableInt!` is an int.
// This throws if aNullableInt is null.

Dart相关问答推荐

有没有办法否定IF条件中的模式大小写匹配?

dart中如何从xpath获取属性值?

根据 Id 比较两个不同的 List

将所有内容放在有序组中,但将括号中的字符组合在一起

在Flutter中重命名文件/图像

无法在 IOS 上使用 firebase 运行Flutter应用程序

Flutter Expansion Tile -- 标题 colored颜色 变化和尾随动画箭头 colored颜色 变化

我可以动态应用 Dart 的字符串插值吗?

InheritedWidget - 在 navigator.push 之后在 null 上调用 getter

如何创建一个对话框,能够接受文本输入并在Flutter中显示结果?

Flitter中的谷歌 map 没有出现

Visual Studio 的 Dart 插件

如何提高数据与二进制数据转换的 Dart 性能?

Dart - 将自纪元以来的毫秒数(UNIX 时间戳)转换为人类可读的时间

Dart:Iterable 与 List,总是使用 Iterable?

在 DART 中创建泛型类型的实例

Dart lambda/shortland 函数混淆

Dart 中的错误与异常

dart 折叠(Fold)与减少(Reduce)

在 Dart 中为数字添加前导零