【发布时间】:2020-02-03 11:46:57
【问题描述】:
我编写了一些代码,用于帮助使用自定义绘制工具可视化百分比,但有时百分比太小而无法正确显示,因此我决定制作一个方便地返回一个值的 getter,该值将被放入画家。
正如您在提供的代码中看到的那样,我尝试进行检查以检查数据是否尚未加载,并返回“0%”,这应该有效。
double get winPercent {
var initalValue = _stats.winPercent.isNotEmpty ? _stats.winPercent : '0%';
var calculateWinPrcnt = (double.parse(
initalValue.substring(0, _stats.winPercent.length - 1),
) /
100);
if (calculateWinPrcnt < 5) {
return 0.05;
}
return calculateWinPrcnt;
}
如果 API 还没有数据,getter 应该返回 '0%',但我仍然收到输出为 null 的错误
The getter 'winPercent' was called on null.
Receiver: null
Tried calling: winPercent
【问题讨论】: