💎

[dart] ?? 記法について(if null記法)

2021/12/06に公開

Dartにおいて、??は「if null」を意味します。

String a = b ?? 'hello';
  1. b = null の場合、 a = 'hello' となる。
  2. b = 'ok' の場合、 a = 'ok' となる。
b ??= 'hello';

こちらも1・2と同じく、b = nullの場合、b = 'hello'となる。

その他のパターン

※ Dart 2.14

  • ?? ... if null operator
  • ??= ... null-aware assignment
  • x?.p ... null-aware access
  • x?.m() ... null-aware method invocation

参考

https://dart.dev/guides/language/language-tour#operators

Discussion