Skip to main content

avoid-empty-setstate

added in: 1.3.0
Pro+
preset: recommended

Warns when a setState callback is empty.

Calling setState with an empty callback will still cause the widget to be re-rendered, but since it does not change the state, an empty callback is usually a sign of a bug.

Example

❌ Bad:

class _FooState extends State<StatefulWidget> {
Widget build(context) {
return FooWidget(
onChange: (value) async {
// LINT: Avoid calling 'setState' with an empty callback. Try updating the callback or removing this invocation.
setState(() {});
},
);
}
}

✅ Good:

class _FooState extends State<StatefulWidget> {
String _myState;

Widget build(context) {
return FooWidget(
onChange: (value) async {
setState(() {
_myState = 'changed';
});
},
);
}
}

Additional Resources