Skip to main content

avoid-recursive-widget-calls

added in: 1.8.0
Pro+
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();
}
}