avoid-unnecessary-collections
preset: recommended
Warns when a collection literal can be replaced by its first and only element.
Example
❌ Bad:
Future<void> fn() async {
await Future.wait([future]); // LINT: Avoid unnecessary collections. Try awaiting this 'Future' directly.
await Future.any([future]); // LINT: Avoid unnecessary collections. Try awaiting this 'Future' directly.
_array.addAll([value]); // LINT: Avoid unnecessary collections. Try using '.add' instead.
_array.addAll({value}); // LINT: Avoid unnecessary collections. Try using '.add' instead.
Stream.fromFutures([future]); // LINT: Avoid unnecessary collections. Try using '.fromFuture' instead.
Stream.fromIterable([value]); // LINT: Avoid unnecessary collections. Try using '.value' instead.
set.addAll([value]); // LINT: Avoid unnecessary collections. Try using '.add' instead.
set.containsAll([value]); // LINT: Avoid unnecessary collections. Try using '.contains' instead.
}
✅ Good:
Future<void> fn() async {
await future;
_array.add(value); // Correct, 'add' instead of 'addAll'
Stream.fromFuture(future);
Stream.value(value);
set.add(value);
set.contains(value);
}