max_widget_nesting
Reports a widget expression that nests its widgets deeper than the limit. Move a part into a widget class with a name.
- Tier
- advisory
- Scope
- Flutter
- Fix
- none
What it reports
Section titled “What it reports”A widget expression whose widgets stand inside each other deeper than the limit max. An expression exactly at max is not reported.
An expression has at most one violation, however many of its widgets are too deep. It is on the constructor name of the outermost widget, Scaffold in Scaffold(…) or ListView.builder in ListView.builder(…), and its message names the depth and the limit:
This widget expression nests its widgets 12 levels deep, which is over the limit of 10.
How the depth is counted, in short (in full under How the depth is counted):
- Each widget constructor call is a level. The outermost widget is level 1. Children side by side are at the same level: a
Columnis 1 plus the depth of its deepest child. - Lists, collection
ifandfor, conditional expressions and builder callbacks add no level. The widgets in them count as written in the argument, and the deepest branch counts. - An event callback starts again. A widget built in an
onPressedor anonTapstarts an expression of its own, at level 1. - A widget a method returns, or one held by a variable, a field or a parameter, is not followed.
The rule reports nothing in a package that does not use Flutter.
A widget twelve levels in is read through eleven constructor calls, their arguments and their closing brackets, and a change to one inner part touches a build that holds the whole screen; that part cannot be named, reused or tested alone. Code written by an AI coding agent grows this way readily, by wrapping what is there in one more widget; a limit makes it give a part a name. The rule is advisory because depth is a signal, not a proof: a tree of twelve single-child wrappers is plain to read.
Example
Section titled “Example”max: 5@overrideWidget build(BuildContext context) { return Scaffold( body: SafeArea( child: Column( children: [ Padding( padding: const EdgeInsets.all(16), child: Row( children: [ CircleAvatar(child: Text(user.initials)), Expanded(child: Text(user.name)), ], ), ), Expanded(child: OrderList(orders: orders)), ], ), ), );}Scaffold, SafeArea, Column, Padding, Row, CircleAvatar or Expanded, Text.@overrideWidget build(BuildContext context) { return Scaffold( body: SafeArea( child: Column( children: [ UserHeader(user: user), Expanded(child: OrderList(orders: orders)), ], ), ), );}
class UserHeader extends StatelessWidget { const UserHeader({super.key, required this.user});
final User user;
@override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.all(16), child: Row( children: [ CircleAvatar(child: Text(user.initials)), Expanded(child: Text(user.name)), ], ), ); }}The screen now has depth 5, the header depth 4, and the header has a name. A widget class is a better home for the part than a method that returns a widget: Flutter can skip rebuilding a widget whose arguments did not change, but cannot skip a method call.
Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
max |
none in the rule; the presets give it | The largest number of widget constructor calls that may stand one inside the other in a widget expression. A whole number, zero or more. |
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
The rule has no limit of its own. The presets give one:
| Preset | max |
|---|---|
core |
the rule is not in this preset |
recommended |
10 |
strict |
7 |
To change it:
assay: rules: max_widget_nesting: max: 12Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression. It goes above the constructor name of the outermost widget, where the violation is, or at the end of that line:
// The generated form mirrors the nesting of the schema it is built from.// ignore: assay/max_widget_nestingreturn Scaffold(// ignore_for_file: assay/max_widget_nesting silences it in a whole file, and // ignore: max_widget_nesting without the prefix works too. See Suppressions.
Details
Section titled “Details”All cases
How the depth is counted
Section titled “How the depth is counted”The depth is the largest number of widget constructor calls that stand one inside the other in the expression.
A level is a widget constructor call, and nothing else is: a call of a constructor, unnamed or named, with const or without, of a class that extends Flutter’s Widget, in Flutter or in the project. A widget created by a dot shorthand, .new(…) or .named(…) where a widget type is expected, is no level; what is written in its arguments is still looked at. A widget in a constructor’s initializer list, super(child: Padding(…)), starts an expression: the super call is no widget constructor call.
The outermost widget is at level 1. A widget written anywhere in an argument of another is one level below it. Widgets side by side, as the children of one list, are at the same level: a Column is 1 plus the depth of its deepest child, however many children it has.
What stands between two widget calls adds no level, and the widgets in it count as written directly in the argument:
| Between the two | Effect |
|---|---|
| A list, set or map literal | No level. Each element is looked at. |
An if, a for or a spread in a collection literal |
No level. Every branch is looked at, and the deepest counts. |
A conditional expression, ??, a switch expression, a cast, parentheses |
No level. Every branch is looked at, and the deepest counts. |
| A builder callback: a function that returns a widget or a collection of widgets, with an expression body or a block body | No level. Every widget constructor call in its body is one level below the widget the callback is an argument of, also one that is first put into a local variable there. |
A call that creates no widget: of a function, a method, or a constructor of another class (items.map(…).toList(), wrap(Text('a')), ValueKey(…)) |
No level for the call. The widget constructor calls in its arguments are looked at. |
The expression ends where the source leaves it:
- At a function that returns something other than a widget or a collection of widgets, such as an event callback (
onPressed,onTap). A widget created in it starts an expression of its own, at level 1, measured and reported by itself. A dialog built inside anonPressedis such an expression. A function that returns a future of a widget, or a widget in a record or a map, ends the expression too. - At a call of a function or method that returns a widget. The call adds no level, and what the function builds is not followed: it is an expression of its own, where it is written.
- At a name. A widget held by a variable, a field or a parameter is not followed, nor is a function passed by name,
builder: _buildRow.
So this expression has depth 6: Scaffold, Padding, Column, Expanded, ListView.builder, and the Text the builder callback returns. The Text in the if is at level 4, beside the Expanded.
Scaffold( // 1 body: Padding( // 2 padding: const EdgeInsets.all(8), // no widget, no level child: Column( // 3 children: [ if (title != null) Text(title), // 4 Expanded( // 4 child: ListView.builder( // 5 itemBuilder: (context, index) => Text(items[index]), // 6 ), ), ], ), ),)Rule parameter fine print
Section titled “Rule parameter fine print”Where no limit is in effect, the rule reports nothing.
The depth is the rule’s own count. It is not one of the metrics, and the metrics report does not list it.