avoid-returning-widgets
Warns when a method, function or getter returns a Widget or subclass of a Widget.
Extracting widgets to a method is considered as a Flutter anti-pattern, because when Flutter rebuilds widget tree, it calls the function all the time, making more processor time for the operations.
Consider creating a separate widget instead of a function or method.
info
The following patterns will not trigger the rule:
- Widget
build
method overrides. - Class method that is passed to a builder.
- Functions with functional_widget package annotations.
⚙️ Config
Set ignored-names
(default is empty) to ignore a function or method name.
Set ignored-annotations
(default is [FunctionalWidget
, swidget
, hwidget
, hcwidget
]) to override the default list of ignored annotations.
Set allow-nullable
(default is false
) to allow functions or methods with the nullable return type (example).
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-returning-widgets:
ignored-names:
- testFunction
ignored-annotations:
- allowedAnnotation
allow-nullable: false
Example
❌ Bad:
class MyWidget extends StatelessWidget {
const MyWidget();
// LINT: Avoid returning widgets from a getter. Try replacing this getter with a stateless widget.
Widget get _someWidget => Container();
Widget _buildShinyWidget() {
return Container(
child: Column(
children: [
Text('Hello'),
...
],
),
);
}
Widget build(BuildContext context) {
return Row(
children: [
Text('Text!'),
...
// LINT: Avoid returning widgets from a function. Try replacing this function with a stateless widget.
_buildShinyWidget(),
],
);
}
}
✅ Good:
class MyWidget extends StatelessWidget {
const MyWidget();
Widget build(BuildContext context) {
return Row(
children: [
Text('Text!'),
...
const _MyShinyWidget(),
],
);
}
}
class _MyShinyWidget extends StatelessWidget {
const _MyShinyWidget();
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Text('Hello'),
...
],
),
);
}
}
✅ Good:
class MyWidget extends StatelessWidget {
Widget _buildMyWidget(BuildContext context) {
return Container();
}
Widget build(BuildContext context) {
return Builder(
builder: _buildMyWidget, // Correct, used in a builder
);
}
}
Example with "allow-nullable"
Config
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-returning-widgets:
allow-nullable: true
✅ Good:
class MyWidget extends StatelessWidget {
Widget build(BuildContext context) {
return Row(
children: [
Text('Text!'),
...
// Correct, allowed
if (_globalBuildMyWidget() != null) _globalBuildMyWidget(),
],
);
}
}
Widget? _globalBuildMyWidget() {
return Container();
}
Additional Resources
- https://github.com/flutter/flutter/issues/19269
- https://flutter.dev/docs/perf/rendering/best-practices#controlling-build-cost
- https://www.reddit.com/r/FlutterDev/comments/avhvco/extracting_widgets_to_a_function_is_not_an/
- https://medium.com/flutter-community/splitting-widgets-to-methods-is-a-performance-antipattern-16aa3fb4026c