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
What it reports
Section titled “What it reports”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
Stateitself, with no mixin; - declares no instance field;
- overrides none of
initState,didChangeDependencies,didUpdateWidget,activate,deactivate,reassembleanddispose; - calls no
setStateand reads nomounted; - 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.
Example
Section titled “Example”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');}class Label extends StatelessWidget { const Label({super.key});
@override Widget build(BuildContext context) => const Text('label');}Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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.