prefer_is_not_empty
对 Iterable
和 Map
使用 isNotEmpty
。
详细信息
#对于 Iterable
和 Map
实例,优先使用 x.isNotEmpty
,而非 !x.isEmpty
。
在测试可迭代对象或映射是否为空时,优先使用 isNotEmpty
而非 !isEmpty
,以提高代码可读性。
差
dart
if (!sources.isEmpty) {
process(sources);
}
佳
dart
if (todo.isNotEmpty) {
sendResults(request, todo.isEmpty);
}
启用
#要启用 prefer_is_not_empty
规则,请在 analysis_options.yaml
文件中的 linter > rules 下添加 prefer_is_not_empty
。
analysis_options.yaml
yaml
linter:
rules:
- prefer_is_not_empty
如果你使用 YAML 映射语法配置 Linter 规则,请在 linter > rules 下添加 prefer_is_not_empty: true
。
analysis_options.yaml
yaml
linter:
rules:
prefer_is_not_empty: true