avoid-unnecessary-stateful-widgets

Path
lint/correctness/avoid-unnecessary-stateful-widgets
Default
On (recommended)
Origin
Falcon
Domain
flutter
Rule
173 of 512
recommendedflutter

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

example.dartdart
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: []); }
This widget has no state or lifecycle behavior; use `StatelessWidget`.
1import 'package:flutter/material.dart';
2class A extends StatefulWidget { State<A> createState() => AState(); }
This widget has no state or lifecycle behavior; use `StatelessWidget`.
3class AState extends State<A> { Widget build(BuildContext context) => Container(); }
4class B extends StatefulWidget { State<B> createState() => BState(); }
This widget has no state or lifecycle behavior; use `StatelessWidget`.
5class BState extends State<B> { Widget build(BuildContext context) => Text('B'); }
6class C extends StatefulWidget { State<C> createState() => CState(); }
This widget has no state or lifecycle behavior; use `StatelessWidget`.
7class CState extends State<C> { Widget build(BuildContext context) { return Container(); } }
8class D extends StatefulWidget { State<D> createState() => DState(); }
This widget has no state or lifecycle behavior; use `StatelessWidget`.
9class DState extends State<D> { Widget build(BuildContext context) => Row(children: []); }
10class E extends StatefulWidget { State<E> createState() => EState(); }

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-unnecessary-stateful-widgets": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/avoid-unnecessary-stateful-widgets: <reason>