avoid-unnecessary-stateful-widgets
preset: recommended
Warns when a StatefulWidget
can be converted to a StatelessWidget
.
Using a StatelessWidget
where appropriate leads to more compact, concise, and readable
code and is more idiomatic.
note
You can fix this rule's issues using the "Convert to StatelessWidget" assist.
Example
❌ Bad:
// LINT: This widget can be converted to a 'StatelessWidget'.
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
Widget build(BuildContext context) {
return Container();
}
}
✅ Good:
class MyWidget extends StatelessWidget {
const MyWidget({super.key});
Widget build(BuildContext context) {
return Container();
}
}