String checkList(List list) {
if (list?.isEmpty) {
return 'Got nothing';
}
return 'Got something';
}
因为 list?.isEmpty 的值,可能是空。
而if判断的必须是非空的bool
转成下面就不报错了:
String checkList(List? list) {
bool? isEmpty = list?.isEmpty;
if(isEmpty == null){
isEmpty = true;
}
if (isEmpty) {
return 'Got nothing';
}
return 'Got something';
}
或者简化如下:
String checkList(List? list) {
if (list?.isEmpty ?? true) {
return 'Got nothing';
}
return 'Got something';
}