“!”之间有什么区别?和 ”?” dart null 安全中的运算符?

What's the differences between "!" and "?" operators in dart null safety?

谁能解释一下两者之间的区别 使用这个:

final block = blocks?.first;

还有这个:

final block = blocks!.first;

其中 blocks 是:

List<Block>? blocks

当你使用blocks?.first时,blocks的值可以是null,但是当你使用blocks!.first,你通知编译器你确定 blocksnot null.

final block = blocks!.first; 表示您完全确信 List<Block>? blocks 在块分配之前已初始化。

也在 final block = blocks?.first; 中,block 可为空 ,但在 final block = blocks!.first;block 不可为 null

  List<Block>? blocks;
  ...
  // you are not sure blocks variable is initialized or not.
  // block is nullable.
  final Block? block = blocks?.first;

  // you are sure blocks variable is initialized.
  // block is not nullable.
  final Block block = blocks!.first;