2021/12/9 : Dart : Conditional member access ( ?. )について

 

(この記事、というかこのブログ全体で、言葉の定義として

フィールド = プロパティ

です。)

Conditional member access

nullableな変数に対して使うことで実行時エラーを防げる。

//sample_1
void main() {
  P? p1;
  
  int n = p1.y; //←error
  print('$n');
  
}

class P{
  int y=0;
}

 

//result of sample_1
line 4 • The property 'y' can't be