【发布时间】:2021-12-14 00:28:18
【问题描述】:
我对 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++;
【问题讨论】:
-
i++在循环的第一次迭代中应该做什么?使i不可为空并首先对其进行初始化:int i = 0;。
标签: flutter dart dart-null-safety