Skip to main content

avoid-misused-set-literals

added in: 1.21.0
Pro+

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

Example

❌ Bad:

void fn() {
wrong(() => {1}); // LINT
wrong(() async => {1}); // LINT
wrongNamed(callback: () => {1}); // LINT

final void Function() wrong = () => {1}; // LINT
}

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

void wrong() => {1}; // LINT

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


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

✅ Good:

void fn() {
correct(() => 1);
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: () {
setState(() {
value = '2';
});
},
);
}
}