我對 flutter 還很陌生,并且由于 null 安全功能,我從可以在 Java 或其他語言中完美運行的代碼中得到了很多錯誤。例如這個——
int ? i;
var icecreamFlavours = ['chocolate', 'vanilla', 'orange'];
icecreamFlavours.forEach((item) {
i ; //This is where I get the error
print('We have the $item flavour');
});
我的錯誤資訊
Error: Operator ' ' cannot be called on 'int?' because it is potentially null.
i ;
Error: A value of type 'num' can't be assigned to a variable of type 'int?'.
i ;
uj5u.com熱心網友回復:
嘗試這個:
i = i! 1
int?can intornull and for last 不定義增量操作。
Github 執行緒:可空復合賦值不方便。
語言規范:Dart 空斷言復合賦值
uj5u.com熱心網友回復:
您不能在可空變數上執行i 或執行任何操作i =1,因為原因很簡單,空變數可以具有空值,這將使增量毫無意義。
除此之外,你甚至不能做i! 或者i! =1即使你確定它i不為空。這個https://github.com/dart-lang/language/issues/1113有一個未解決的問題
所以現在你可以這樣做 i = i! 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340225.html
