目的
Optionalな変数をいつまでも!つけて強制アンラップして使用し続けるのが精神上よくないのでアンラップしたい
方法
一度、if文でnull判定を書くと、それ以降はアンラップされたとみなされるようです。
// Dart
String? x;
... // Assign some values or null to x.
// print(x.length); // This would NOT compile.
if (x == null) {
// x is `null`
return;
}
// x is non-null, and the compiler knows that.
print(x.length); // This will compile.
ただSwiftのguardみたいに文法上アンラップする方法があるといいな。