Skip to main content

avoid_returning_widgets

A rule which warns about returning widgets from functions and methods.

Using functions instead of Widget subclasses for decomposing Widget trees may cause unexpected behavior and performance issues.

Exceptions:

  • overriden methods

More details: https://github.com/flutter/flutter/issues/19269

Example

BAD:

Widget avoidReturningWidgets() => const SizedBox(); // LINT

class MyWidget extends StatelessWidget {
Widget get box => SizedBox(); // LINT
Widget test1() => const SizedBox(); //LINT
Widget get _test3 => const SizedBox(); // LINT
}

GOOD:

class MyWidget extends MyWidget {


Widget test1() => const SizedBox();


Widget get box => ColoredBox(color: Colors.pink);


Widget build(BuildContext context) {
return const SizedBox();
}
}

Parameters

exclude (List<AvoidReturningWidgetsExclude>)

A list of methods that should be excluded from the lint.