Skip to main content

avoid-misused-set-literals

added in: 1.21.0
Pro+
preset: recommended

Warns when a set literal is used in a wrong place.

Example

❌ Bad:

void fn() {
wrong(() => {1}); // LINT: Avoid misused set literals. Try removing the set literal around the expression.
wrong(() async => {1}); // LINT: Avoid misused set literals. Try removing the set literal around the expression.
wrongNamed(callback: () => {1}); // LINT: Avoid misused set literals. Try removing the set literal around the expression.

final void Function() wrong = () => {1}; // LINT: Avoid misused set literals. Try removing the set literal around the expression.
}

void wrongNamed({required void Function() callback}) {}
void wrong(void Function() p) {}

void wrong() => {1}; // LINT: Avoid misused set literals. Try removing the set literal around the expression.

class _State<T> extends State<T> {
String value;


Widget build() {
return MyWidget(
// LINT: Avoid misused set literals. Try removing the set literal around the expression.
onPressed: () => {
setState(() {
value = '2';
})
},
);
}
}

✅ Good:

void fn() {
correct(() => 1); // Correct, no set literal
correct(() async => 1);
correctNamed(callback: () => 1);

final void Function() correct = () => 1;
// or
final void Function() correct = () { 1; };
}

void correctNamed({required void Function() callback}) {}
void correct(void Function() p) {}

void correct() {
print('');
};

class _State<T> extends State<T> {
String value;


Widget build() {
return MyWidget(
onPressed: () { // Correct, '=>' is removed
setState(() {
value = '2';
});
},
);
}
}