Skip to content

stateful_widget_without_state

Reports a StatefulWidget whose State holds no state. Make it a StatelessWidget.

Tier
advisory
Scope
Flutter
Lowest preset
recommended
Fix
none

A StatefulWidget, not abstract, whose createState returns a state class of the same file that holds no state. The violation covers the widget’s class name.

A state class holds no state when it:

  • is private and extends Flutter’s State itself, with no mixin;
  • declares no instance field;
  • overrides none of initState, didChangeDependencies, didUpdateWidget, activate, deactivate, reassemble and dispose;
  • calls no setState and reads no mounted;
  • is named nowhere else in the file or in another part of its library, such as by a GlobalKey.

The rule is silent in a package that does not use Flutter.

The widget costs a state object and an element of its own for what a StatelessWidget builds just as well. It also tells the reader there is state where there is none.

Violating
class Label extends StatefulWidget {
const Label({super.key});
@override
State<Label> createState() => _LabelState();
}
class _LabelState extends State<Label> {
@override
Widget build(BuildContext context) => const Text('label');
}
Corrected
class Label extends StatelessWidget {
const Label({super.key});
@override
Widget build(BuildContext context) => const Text('label');
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why this place is fine.
// ignore: assay/stateful_widget_without_state

// ignore_for_file: assay/stateful_widget_without_state silences it in a whole file, and // ignore: stateful_widget_without_state without the prefix works too. See Suppressions.