avoid-unnecessary-stateful-widgets
- Group
- Correctness
- Path
lint/correctness/avoid-unnecessary-stateful-widgets- Default
- On (recommended)
- Origin
- Falcon
- Domain
- flutter
- Rule
- 173 of 512
Warns when a StatefulWidget's local State contains only a pure build method.
Both declarations must resolve to Flutter types in this file. State fields,
mixins, lifecycle methods, extra instance members, and any State member use
other than widget conservatively retain the StatefulWidget. Inherited custom
State bases are ignored because they may own lifecycle behavior.
Invalid
import 'package:flutter/material.dart';
class A extends StatefulWidget { State<A> createState() => AState(); }
class AState extends State<A> { Widget build(BuildContext context) => Container(); }
class B extends StatefulWidget { State<B> createState() => BState(); }
class BState extends State<B> { Widget build(BuildContext context) => Text('B'); }
class C extends StatefulWidget { State<C> createState() => CState(); }
class CState extends State<C> { Widget build(BuildContext context) { return Container(); } }
class D extends StatefulWidget { State<D> createState() => DState(); }
class DState extends State<D> { Widget build(BuildContext context) => Row(children: []); }
class E extends StatefulWidget { State<E> createState() => EState(); }
class EState extends State<E> { Widget build(BuildContext context) => Column(children: []); }import 'package:flutter/material.dart';class A extends StatefulWidget { State<A> createState() => AState(); }class AState extends State<A> { Widget build(BuildContext context) => Container(); }class B extends StatefulWidget { State<B> createState() => BState(); }class BState extends State<B> { Widget build(BuildContext context) => Text('B'); }class C extends StatefulWidget { State<C> createState() => CState(); }class CState extends State<C> { Widget build(BuildContext context) { return Container(); } }class D extends StatefulWidget { State<D> createState() => DState(); }class DState extends State<D> { Widget build(BuildContext context) => Row(children: []); }class E extends StatefulWidget { State<E> createState() => EState(); }Valid
import 'package:flutter/material.dart';
class A extends StatefulWidget { State<A> createState() => AState(); }
class AState extends State<A> { int count = 0; Widget build(BuildContext context) => Text('$count'); }
class B extends StatefulWidget { State<B> createState() => BState(); }
class BState extends State<B> { void initState() { super.initState(); } Widget build(BuildContext context) => Container(); }
class C extends StatefulWidget { State<C> createState() => CState(); }
class CState extends State<C> { void dispose() { super.dispose(); } Widget build(BuildContext context) => Container(); }
class D extends StatefulWidget { State<D> createState() => DState(); }
class DState extends State<D> { Widget build(BuildContext context) => Text('$mounted'); }
class E extends StatelessWidget { Widget build(BuildContext context) => Container(); }
class Unknown extends StatefulWidget { State<Unknown> createState() => externalState(); }Configure avoid-unnecessary-stateful-widgets
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-unnecessary-stateful-widgets": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-unnecessary-stateful-widgets: <reason>More correctness rules
- avoid-unreachable-for-loop
Reports iteration of a collection in a branch that requires it to be empty.
- avoid-unrelated-type-castsrecommended
Flags casts between types proven unrelated by canonical type resolution.
- avoid-unsafe-collection-methods
Flags potentially throwing element access on SDK collections.
- avoid-unsafe-reduce
Warn when SDK Iterable.reduce can receive an empty collection.
- avoid-unused-after-null-check
Reports a null-checked local ignored by a branch that passes another local of the same resolved type to a call.
- avoid-unused-parameters
avoid-unused-parametersrecommendedDisallow declared parameters that are never used, excluding primary constructors.
- avoid-watch-outside-buildrecommendedprovider
Flags listening Provider accesses proven to run outside Flutter builds.
- avoid-web-libraries-in-flutter
avoid_web_libraries_in_flutterrecommendedflutterDisallow importing web-only
dart:libraries from Flutter code.