widget_builds_itself
Reports a widget that creates its own class in its own build with no condition, which recurses until the stack overflows. Build something else, or put the call under a condition that ends.
- Tier
- blocking
- Scope
- Flutter
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”A constructor call of a widget’s own class in its own build, where nothing decides whether it runs:
- the
buildof aStatelessWidgetthat creates the same class; - the
buildof aStatethat creates its widget class.
The violation covers the constructor’s name.
It does not report a call:
- inside a function written in the
build, such as a builder or an event callback; - under an
if, a conditional expression, aswitch, a loop or acatch; - on the right side of
&&,||or??; - after an
ifthat can leave thebuild.
The rule is silent in a package that does not use Flutter.
The widget is built again inside itself without end, until the stack overflows. A recursive widget, such as a tree of comments, creates a smaller one of itself under a condition that ends the repetition. The rule leaves that alone.
Example
Section titled “Example”class Badge extends StatelessWidget { const Badge({super.key});
@override Widget build(BuildContext context) => const Badge();}class Badge extends StatelessWidget { const Badge({super.key});
@override Widget build(BuildContext context) => const Icon(Icons.star);}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/widget_builds_itself// ignore_for_file: assay/widget_builds_itself silences it in a whole file, and // ignore: widget_builds_itself without the prefix works too. See Suppressions.