avoid_empty_setstate
This rule flags setState(() {}) — a call whose callback contains no statements.
Why use this rule
Section titled “Why use this rule”An empty callback means the state was already mutated somewhere else and setState is being used purely as a “please rebuild” signal. It works, but it separates the mutation from the rebuild request, which causes three problems:
- Readers cannot see what changed by looking at the
setStatecall. - The framework’s debug assertions, which run around the callback, no longer bracket the actual mutation.
- If the mutation later moves after the
setStatecall, the rebuild silently renders stale values.
Putting the mutation inside the callback fixes all three and costs nothing.
See also: State.setState docs
void increment() { _counter++; // The rebuild request is detached from the change setState(() {});}void increment() { setState(() { _counter++; });}Known limitations
Section titled “Known limitations”Only a genuinely empty block is reported. A callback containing any statement is left alone, even if that statement has no effect — proving a statement is a no-op is outside this rule’s scope.
Turning this rule off
Section titled “Turning this rule off”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_empty_setstate: trueTo turn it off again:
rules: avoid_empty_setstate: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Options
Section titled “Options”Projects with a state abstraction that does not extend Flutter’s State can
opt that base class into this rule:
many_lints: rules: avoid_empty_setstate: state_base_classes: [AppState]rules: avoid_empty_setstate: state_base_classes: [AppState]| Option | Type | Default | Description |
|---|---|---|---|
state_base_classes |
list of strings | [] |
Additional non-State base classes whose subclasses should be treated as state classes |
Related rules
Section titled “Related rules”avoid_mounted_in_setstate— Detect mounted checks inside setState callbacks.avoid_unnecessary_setstate— Detect unnecessary setState calls in lifecycle methods.avoid_inherited_widget_in_initstate— Don’t look up inherited widgets inside initState.avoid_late_context— Don’t read BuildContext in a late field initializer.