avoid-unnecessary-collections
- Group
- Performance
- Path
lint/performance/avoid-unnecessary-collections- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 157 of 512
Avoid singleton collection literals where SDK APIs accept a single value.
Only plain singleton lists/sets and semantically resolved SDK receivers are
checked. Collection control flow, spreads, maps, dynamic/custom APIs and named
arguments are excluded. Future.wait is checked only for a discarded awaited
result, because replacing it changes List
Invalid
import 'dart:async';
import 'dart:async' as async;
void append(List<int> list, Set<int> set) {
list.addAll([1]);
list.addAll({2});
set.addAll([3]);
set.addAll({4});
set.containsAll({5});
}
Future<void> futures(Future<int> value) async {
await Future.wait([value]);
await async.Future.any([value]);
}
Stream<int> fromFuture(Future<int> value) => Stream.fromFutures([value]);
Stream<int> fromValue(int value) => async.Stream.fromIterable([value]);
Future<int> first(Future<int> value) {
return Future.any([value]);
}
Future<int> assigned(Future<int> value) {
final result = Future.any([value]);
return result;
}
void awaitedArrow(Future<int> value) async => Future.any([value]);
void consumedSwitch(Future<int> value) => switch (Future.any([value])) {
final result => result.ignore(),
};void append(List<int> list, Set<int> set) { list.addAll([1]); list.addAll([1]); list.addAll({2}); list.addAll({2}); set.addAll([3]); set.addAll([3]); set.addAll({4}); set.addAll({4}); set.containsAll({5});Future<void> futures(Future<int> value) async { await Future.wait([value]); await Future.wait([value]); await async.Future.any([value]);}Stream<int> fromFuture(Future<int> value) => Stream.fromFutures([value]);Stream<int> fromFuture(Future<int> value) => Stream.fromFutures([value]);Stream<int> fromValue(int value) => async.Stream.fromIterable([value]);Future<int> first(Future<int> value) { return Future.any([value]);Future<int> assigned(Future<int> value) { final result = Future.any([value]);}void awaitedArrow(Future<int> value) async => Future.any([value]);void awaitedArrow(Future<int> value) async => Future.any([value]);void consumedSwitch(Future<int> value) => switch (Future.any([value])) {Valid
import 'dart:async';
class Custom {
void addAll(Object value) {}
bool containsAll(Object value) => true;
}
void append(List<int> list, Set<int> set, Map<int, int> map,
Iterable<int> source, dynamic unknown, bool condition) {
list.addAll([]);
list.addAll([1, 2]);
list.addAll([...source]);
list.addAll([if (condition) 1]);
list.addAll([for (final value in source) value]);
set.containsAll(source);
map.addAll({1: 2});
unknown.addAll([1]);
Custom().addAll([1]);
Custom().containsAll({1});
}
Future<List<int>> used(Future<int> value) async {
return await Future.wait([value]);
}
Future<void> options(Future<int> value) async {
await Future.wait([value], eagerError: true);
await Future.wait([value], cleanUp: (value) {});
}
Stream<int> multiple(Future<int> first, Future<int> second) => Stream.fromFutures([first, second]);
void shadow(dynamic Future, dynamic Stream) {
Future.any([1]);
Stream.fromIterable([1]);
}
void discarded(Future<int> value) {
Future.any([value]);
(Future.any([value]));
Future.any([value])..ignore();
}
void discardedArrow(Future<int> value) => Future.any([value]);Configure avoid-unnecessary-collections
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"avoid-unnecessary-collections": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/avoid-unnecessary-collections: <reason>More performance rules
- avoid-unnecessary-consumer-widgetsriverpod
Flags Riverpod consumer widgets whose build reference is unused.
- avoid-unnecessary-futures
Prefer synchronous declarations when an async Future only returns scalars.
- avoid-unnecessary-gesture-detectorflutter
Flags handler-free Flutter
GestureDetectorsand tap-only wrappers around enabled Flutter buttons. - avoid-unnecessary-hook-widgetsflutter_hooks
Flags confirmed hook widgets and
HookBuildercallbacks that do not use hooks. - move-variable-closer-to-its-usage
Move final/const scalar literals into their only branch, or after an if.
- move-variable-outside-iteration
Reports loop-local final variables whose computation is independent of the loop.
- noop-primitive-operations
noop_primitive_operationsrecommendedFlags primitive operations that return their receiver unchanged.
- prefer-const-border-radius
prefer-const-border-radiusrecommendedflutterFlags
BorderRadius.only(...)whose four corner radii are all equal.