Skip to content

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

A constructor call of a widget’s own class in its own build, where nothing decides whether it runs:

  • the build of a StatelessWidget that creates the same class;
  • the build of a State that 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, a switch, a loop or a catch;
  • on the right side of &&, || or ??;
  • after an if that can leave the build.

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.

Violating
class Badge extends StatelessWidget {
const Badge({super.key});
@override
Widget build(BuildContext context) => const Badge();
}
Corrected
class Badge extends StatelessWidget {
const Badge({super.key});
@override
Widget build(BuildContext context) => const Icon(Icons.star);
}

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/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.