【发布时间】:2020-12-11 13:15:00
【问题描述】:
如何在 Dart 构造函数中断言参数是否为空?
const CardStackCarousel({
Key key,
this.itemBuilder,
this.itemCount,
int backgroundItemCount,
this.controller,
this.offsetAboveBackcards: 10.0,
this.minScale: 0.8,
this.verticalAxis: false,
}) : backgroundItemCount = backgroundItemCount == null ? itemCount - 1 : backgroundItemCount,
assert(backgroundItemCount < itemCount, 'background item must be less than itemCount'),
super(key: key);
使用上面的代码,我得到一个错误:
The method '<' was called on null.
Receiver: null
当用户没有指定 backgroundItemCount 属性时。所以我的想法是也许我们应该只断言这个属性不为空。但我不知道该怎么做
【问题讨论】: