avoid-collection-mutating-methods
Warns when a mutating method is called on a collection.
By default this rule triggers on every method that has a void
return type, as well as for methods in the methods
config option.
⚙️ Config
Set ignore-local
(default is true
) to exclude local variables.
Set methods
(default is [remove
, removeAt
, removeLast
, putIfAbsent
, update
]) to override the list of methods to check.
Set additional-methods
(default is empty) to add additional methods the list of methods to check.
dart_code_metrics:
...
rules:
...
- avoid-collection-mutating-methods:
ignore-local: true
additional-methods:
- contains
Example
❌ Bad:
void main {
final list = [1, 2, 3];
list.remove(1); // LINT
list.insert(0, 4); // LINT
list.addAll([1, 2, 3]); // LINT
list.removeAt(0); // LINT
list.shuffle(); // LINT
final primitiveMap = Map<int, String>();
check(primitiveMap.addAll); // LINT
}
void check(void Function(Map<int, String>) callback) {}
✅ Good:
void main {
final list = [1, 2, 3];
final newList = [...list, ...[1, 2, 3]];
}