avoid-slow-collection-methods
- Group
- Performance
- Path
lint/performance/avoid-slow-collection-methods- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 143 of 512
Reports filtered iterable searches that have a direct search-method alternative.
On confirmed SDK Iterable APIs, where(test).first, .last, and .single
can use firstWhere, lastWhere, and singleWhere without creating a
filtering iterable. The lastWhere recommendation requires a pure predicate:
a list may search backward and stop early, changing predicate evaluation
order and count. No automatic fix is offered. Lazy expand calls are not
reported: eagerly spreading them could change evaluation timing.
Only explicit, non-null-aware chains with one positional callback match.
Source overrides, unrelated lookalike APIs, and unresolved receivers are
excluded. Arrow closures testing their parameter with is T belong to
prefer-iterable-where-type; length and membership checks belong to
prefer-is-empty and prefer-contains, respectively. No arbitrary sync*
function is classified as slow merely because it is a generator.
Invalid
bool positive(int value) => value > 0;
void search(Iterable<int> values, List<int> list, Set<int> set) {
values.where(positive).first;
values.where(positive).last;
values.where(positive).single;
list.where((value) => value > 2).first;
set.where((value) => value.isEven).last;
(values.where((value) => value < 10)).single;
<int>[1, 2, 3].where(positive).first;
values.map((value) => value + 1).where(positive).first;
}
void typeFilters(Iterable<Object> values, Object other) {
values.where((value) => value is! String).first;
values.where((value) { return value is String; }).first;
values.where((value) => other is String).first;
}void search(Iterable<int> values, List<int> list, Set<int> set) { values.where(positive).first; values.where(positive).first; values.where(positive).last; values.where(positive).last; values.where(positive).single; values.where(positive).single; list.where((value) => value > 2).first; list.where((value) => value > 2).first; set.where((value) => value.isEven).last; set.where((value) => value.isEven).last; (values.where((value) => value < 10)).single; (values.where((value) => value < 10)).single; <int>[1, 2, 3].where(positive).first; <int>[1, 2, 3].where(positive).first; values.map((value) => value + 1).where(positive).first;void typeFilters(Iterable<Object> values, Object other) { values.where((value) => value is! String).first; values.where((value) => value is! String).first; values.where((value) { return value is String; }).first; values.where((value) { return value is String; }).first; values.where((value) => other is String).first;Valid
import 'dart:async';
bool positive(int value) => value > 0;
class Query {
Query where(bool Function(int) predicate) => this;
int get first => 0;
int get last => 0;
int get single => 0;
}
abstract class CustomIterable implements Iterable<int> {
@override
Iterable<int> where(bool Function(int) predicate) => <int>[1];
}
void search(Iterable<int> values, Iterable<Object> objects,
Iterable<int>? nullable, dynamic unknown, Query query, CustomIterable custom) {
values.firstWhere(positive);
values.where(positive);
values.first;
values.where(positive).isEmpty;
values.where(positive).length == 0;
objects.where((value) => value is String).first;
objects.where((value) => (value is int)).last;
objects.where((value) => value is num).single;
nullable?.where(positive).first;
unknown.where(positive).first;
query.where(positive).first;
query.where(positive).last;
query.where(positive).single;
custom.where(positive).first;
}
void membership(List<int> values) {
values.indexOf(1) >= 0;
}
void streamSearch(Stream<int> values) {
values.where(positive).first;
}Configure avoid-slow-collection-methods
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"avoid-slow-collection-methods": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/avoid-slow-collection-methods: <reason>More performance rules
- avoid-unnecessary-collections
Avoid singleton collection literals where SDK APIs accept a single value.
- 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.