avoid-recursive-widget-calls
preset: recommended
Warns when a Widget
is recursively used within the same Widget
.
Recursive use of a widget is most often a sign of a bug and can get your application stuck in an endless loop.
Example
❌ Bad:
class MyHomePage extends StatefulWidget {
const MyHomePage();
_MyHomePageState createState() => _MyHomePageState<MyHomePage>();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build() {
// LINT: Avoid calling the same widget recursively. Try rewriting the code without recursion.
return MyHomePage();
}
}
class MyOtherWidget extends StatelessWidget {
Widget build() {
// LINT: Avoid calling the same widget recursively. Try rewriting the code without recursion.
return MyOtherWidget();
}
}
✅ Good:
class MyHomePage extends StatefulWidget {
const MyHomePage();
_MyHomePageState createState() => _MyHomePageState<MyHomePage>();
}
class _MyHomePageState extends State<MyHomePage> {
Widget build() {
return MyOtherWidget();
}
}
class MyOtherWidget extends StatelessWidget {
Widget build() {
return SomeOtherWidget();
}
}