Skip to main content

avoid-misused-set-literals

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

Example

❌ Bad:

void fn() {
// LINT: Avoid misused set literals. Try removing the set literal around the expression.
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};
}

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

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

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';
});
},
);
}
}