flutter - Dart,后面带有感叹号的标识符

标签 flutter dart

最近我一直在用 flutter 开发移动应用程序,当我查看 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!;
我在前面看到了带有感叹号的 bool 标识符,这意味着它将返回它的相反值,但我从未见过这个。有人能告诉我这是做什么的吗?

最佳答案

这是 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.

关于flutter - Dart,后面带有感叹号的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65749204/

相关文章:

api - 使用 Flutter 向 Flask API 发送 http Post-Request(包含图像)

navigation - 了解 flutter 导航

android - 在Flutter SDK中找不到Dart

android - flutter 找到了多个文件,操作系统独立路径为 'AndroidManifest.xml'

c++ - 使用 dart 实现 winapi(SendInput) :ffi

Dart flutter : How to use multiple statements in ternary inside a flutter widget

flutter - 防止从外部触摸隐藏 showModalBottomSheet

flutter - 条件为 "met"(!snapshot.hasData) 后未返回小部件

android-studio - flutter 卡在等待天文台端口可用

flutter - 导航到另一条路线后如何消费Provider?