Rules / Style / prefer-underscore-for-unused-callback-parameters
prefer-underscore-for-unused-callback-parameters
Flags unused closure parameters that are not named with underscores.
For each closure (function expression), collects every identifier referenced in
its body — including those inside nested closures, cascades, collection literals,
and string interpolations — and flags the first parameter that is never used and
is not already an all-underscore name (_, __). Naming a deliberately ignored
parameter _ signals intent to the reader and avoids the impression that a real
value was forgotten, which matters for the fixed-arity callbacks Flutter APIs
demand (builder: (context, _) => ...). At most one diagnostic is reported per
closure.
Invalid
example.dartdart
// Bad: unused callback parameter should be named _
void example1() {
final items = [1, 2, 3];
items.forEach((item) {
print('hello');
});
}
void example2() {
final map = {'a': 1, 'b': 2};
map.forEach((key, value) {
print('hello');
});
}
void example3() {
final items = ['x', 'y'];
items.map((element) => 'mapped').toList();
}
void example4() {
final numbers = [1, 2, 3];
numbers.where((n) => true).toList();
}
// Bad: unused parameter in reduce-like operation
void example5() {
final values = [1, 2, 3, 4];
values.fold(0, (previous, current) => previous + 1);
}
// Bad: unused index parameter in indexed map
void example6() {
final items = ['a', 'b', 'c'];
items.asMap().forEach((index, value) {
print(value);
});
}
Unused callback parameter should be named '_'.
4 final items = [1, 2, 3];
5 items.forEach((item) {
∙
Unused callback parameter should be named '_'.
11 final map = {'a': 1, 'b': 2};
12 map.forEach((key, value) {
∙
Unused callback parameter should be named '_'.
18 final items = ['x', 'y'];
19 items.map((element) => 'mapped').toList();
∙
Unused callback parameter should be named '_'.
23 final numbers = [1, 2, 3];
24 numbers.where((n) => true).toList();
∙
Unused callback parameter should be named '_'.
29 final values = [1, 2, 3, 4];
30 values.fold(0, (previous, current) => previous + 1);
∙
Unused callback parameter should be named '_'.
35 final items = ['a', 'b', 'c'];
36 items.asMap().forEach((index, value) {
∙
Valid
example.dartdart
// Good: unused callback parameters renamed to _
void example1() {
final items = [1, 2, 3];
items.forEach((_) {
print('hello');
});
}
void example2() {
final map = {'a': 1, 'b': 2};
map.forEach((_, __) {
print('hello');
});
}
void example3() {
final items = ['x', 'y'];
items.map((_) => 'mapped').toList();
}
void example4() {
final numbers = [1, 2, 3];
numbers.where((_) => true).toList();
}
void example5() {
final items = [1, 2, 3];
items.forEach((item) {
print(item);
});
}
void example6() {
final map = {'a': 1, 'b': 2};
map.forEach((key, value) {
print('$key: $value');
});
}
How to configure
Set the severity of prefer-underscore-for-unused-callback-parameters in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-underscore-for-unused-callback-parameters": "error"
}
}
}
}