avoid-unnecessary-setstate
- Group
- Correctness
- Path
lint/correctness/avoid-unnecessary-setstate- Default
- On (recommended)
- Origin
- Falcon
- Domain
- flutter
- Rule
- 172 of 512
Avoid setState during build, initState, or didUpdateWidget. Synchronous local method calls are followed transitively; deferred closures and async methods are excluded.
Invalid
import 'package:flutter/material.dart';
class S extends State<StatefulWidget> { int x=0;
void initState() { setState(() { x++; }); }
Widget build(BuildContext context) { setState(() { x++; }); return Text(''); }
void didUpdateWidget(StatefulWidget old) { setState(() { x++; }); }
}
class Explicit extends State<StatefulWidget> { void initState() { this.setState(() {}); } }
class Helpers extends State<StatefulWidget> { void update() { setState(() {}); } void initState() { update(); } }class S extends State<StatefulWidget> { int x=0;void initState() { setState(() { x++; }); }void initState() { setState(() { x++; }); }Widget build(BuildContext context) { setState(() { x++; }); return Text(''); }Widget build(BuildContext context) { setState(() { x++; }); return Text(''); }void didUpdateWidget(StatefulWidget old) { setState(() { x++; }); }}class Explicit extends State<StatefulWidget> { void initState() { this.setState(() {}); } }class Explicit extends State<StatefulWidget> { void initState() { this.setState(() {}); } }class Helpers extends State<StatefulWidget> { void update() { setState(() {}); } void initState() { update(); } }Valid
import 'package:flutter/material.dart';
class S extends State<StatefulWidget> { int x=0;
void update() { setState(() { x++; }); }
void didChangeDependencies() { setState(() { x++; }); }
Widget build(BuildContext context) { return ElevatedButton(onPressed: () { setState(() { x++; }); }, child: Text('')); }
void initState() { Future.delayed(Duration.zero, () { setState(() { x++; }); }); }
}
class Other { void setState(Function fn) {} void initState() { setState(() {}); } }Configure avoid-unnecessary-setstate
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-unnecessary-setstate": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-unnecessary-setstate: <reason>More correctness rules
- 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.
- 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.