Rules / Correctness / avoid-returning-widgets
avoid-returning-widgets
lint/correctness/avoid-returning-widgetsrecommendedflutter
Same as avoid-returning-widgets · Dart Code MetricsDisallow functions and methods that return a Widget.
Flags a function, method, or getter whose return type is Widget,
StatelessWidget, or StatefulWidget (or a Future, Stream, or
Completer of one). Returning a widget from a helper instead of defining a
real widget class defeats Flutter's element tree: the returned subtree cannot
hold its own state, is rebuilt wholesale whenever the enclosing build runs,
and never appears as a distinct node in the widget inspector. Extract the
subtree into its own StatelessWidget or StatefulWidget so Flutter can
cache and rebuild it independently. The build method and @override
members are exempt, as is any return type naming a concrete widget subclass
such as Container.
Invalid
example.dartdart
// Test cases for avoid-returning-widgets rule
// Flags methods that return Widget type outside of build methods
import 'package:flutter/material.dart';
class MyScreen extends StatelessWidget {
Widget _buildCard() {
return Card(
child: Text('Hello'),
);
}
Widget _buildButton() {
return ElevatedButton(
onPressed: () {},
child: Text('Click me'),
);
}
Widget buildHeader(String title) {
return Container(
child: Text(title),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _buildCard(),
);
}
}
class WidgetHelper {
static Widget createCard() {
return Card(child: SizedBox());
}
static Widget createRow(List<Widget> children) {
return Row(children: children);
}
}
Widget globalWidgetBuilder() {
return Container();
}
Future<Widget> asyncWidgetBuilder() {
return Future.value(Text('async'));
}
Methods should not return Widget directly; extract to a separate widget class
6class MyScreen extends StatelessWidget {
7 Widget _buildCard() {
∙
Methods should not return Widget directly; extract to a separate widget class
12
13 Widget _buildButton() {
∙
Methods should not return Widget directly; extract to a separate widget class
19
20 Widget buildHeader(String title) {
∙
Methods should not return Widget directly; extract to a separate widget class
34class WidgetHelper {
35 static Widget createCard() {
∙
Methods should not return Widget directly; extract to a separate widget class
38
39 static Widget createRow(List<Widget> children) {
∙
Functions should not return Widget directly; extract to a separate widget class
43
44Widget globalWidgetBuilder() {
∙
Functions should not return Widget directly; extract to a separate widget class
47
48Future<Widget> asyncWidgetBuilder() {
∙
Valid
example.dartdart
// Good examples for avoid-returning-widgets rule
// Using separate widget classes instead of methods that return widgets
import 'package:flutter/material.dart';
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: MyCard(),
appBar: AppBar(
title: MyHeader(title: 'Title'),
),
);
}
}
class MyCard extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
child: Text('Hello'),
);
}
}
class MyButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
child: Text('Click me'),
);
}
}
class MyHeader extends StatelessWidget {
final String title;
MyHeader({required this.title});
@override
Widget build(BuildContext context) {
return Container(
child: Text(title),
);
}
}
class WidgetHelper {
static Card createCard() {
return Card(child: SizedBox());
}
static List<Widget> getChildren() {
return [Text('a'), Text('b')];
}
}
void processData(String data) {
print(data);
}
String buildString() {
return "not a widget";
}
How to configure
Set the severity of avoid-returning-widgets in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"correctness": {
"avoid-returning-widgets": "error"
}
}
}
}