avoid_returning_widgets

SeverityQuick FixOptions
Info

Details

Avoid returning widgets from methods.

Static, overridden and extension methods are ignored.

Watch the video below to learn more about the advantages of using widgets as classes instead of methods.

Bad
class A extends StatelessWidget {
  const A({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _buildWidget(),
        _buildWidget(),
      ],
    );
  }

  Widget _buildWidget() {
    return ...;
  }
}
Good
class A extends StatelessWidget {
  const A({super.key});

  @override
  Widget build(BuildContext context) {
    return const Column(
      children: [
        B(),
        B(),
      ],
    );
  }
}

class B extends StatelessWidget {
  const B({super.key});

  @override
  Widget build(BuildContext context) {
    return ...;
  }
}

Usage

To enable the avoid_returning_widgets rule, add avoid_returning_widgets under custom_lint > rules in your analysis_options.yaml file:

custom_lint:
  rules:
    - avoid_returning_widgets

Options

OptionTypeDescriptionDefault Value
ignored_method_namesList<String>A list of method names to ignore[]

To configure the list of ignored method names, add a list of strings to the ignored_method_names option:

custom_lint:
  rules:
    - avoid_returning_widgets:
      ignored_method_names: ['myMethod']