avoid-returning-widgets
Same as avoid-returning-widgets in Dart Code Metrics
- Group
- Correctness
- Path
lint/correctness/avoid-returning-widgets- Default
- Off, opt in
- Origin
- Dart Code Metrics
- Domain
- flutter
- Rule
- 133 of 512
Disallow 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
// 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'));
}class MyScreen extends StatelessWidget { Widget _buildCard() { Widget _buildButton() { Widget buildHeader(String title) {class WidgetHelper { static Widget createCard() { static Widget createRow(List<Widget> children) { Widget globalWidgetBuilder() { Future<Widget> asyncWidgetBuilder() {Valid
// 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";
}Configure avoid-returning-widgets
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-returning-widgets": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-returning-widgets: <reason>More correctness rules
- avoid-type-to-string
avoid_type_to_stringrecommendedAvoid
Type.toString()in production code, where type names may be minified. - avoid-uncaught-future-errorsrecommended
Flags local futures first awaited after another await inside a try/catch.
- avoid-unconditional-break
Reports unconditional break, continue, return or throw in a for-loop body.
- avoid-undisposed-instancesrecommendedflutter
Warns when a disposable constructor result is discarded.
- avoid-unmodified-loop-conditionrecommended
Flags loop conditions whose local scalar inputs are never modified by the loop.
- avoid-unnecessary-setstaterecommendedflutter
Avoid setState during build, initState, or didUpdateWidget.
- avoid-unnecessary-stateful-widgetsrecommendedflutter
Warns when a
StatefulWidget's local State contains only a pure build method. - avoid-unreachable-for-loop
Reports iteration of a collection in a branch that requires it to be empty.