avoid-mounted-in-setstate
- Group
- Correctness
- Path
lint/correctness/avoid-mounted-in-setstate- Default
- Off, opt in
- Origin
- Falcon
- Domain
- flutter
- Rule
- 90 of 512
Check mounted before invoking Flutter State.setState, never inside its callback. A check inside the callback cannot prevent setState's unmounted-State assertion. Deferred nested closures and local functions have independent mounted checks.
Invalid
import 'package:flutter/material.dart' as f;
class S extends f.State<f.StatefulWidget> {
int value = 0;
void update(f.BuildContext other) {
setState(() {
if (mounted) value++;
});
this.setState(() {
if (this.mounted) value++;
});
setState(() {
if (context.mounted) value++;
});
setState((() {
if (!other.mounted) return;
value++;
}));
setState(() => value = mounted ? 1 : 0);
}
}
class Child extends S {
void inherited() {
setState(() { if (mounted) value++; });
}
} setState(() { if (mounted) value++; this.setState(() { if (this.mounted) value++; setState(() { if (context.mounted) value++; setState((() { if (!other.mounted) return; })); setState(() => value = mounted ? 1 : 0); void inherited() { setState(() { if (mounted) value++; });Valid
import 'dart:async';
import 'package:flutter/material.dart' as f;
class Flag { bool get mounted => true; }
class S extends f.State<f.StatefulWidget> {
int value = 0;
void subscribe(Stream<int> stream) {
setState(() {
stream.listen((event) {
if (!mounted) return;
value = event;
});
void later() {
if (!context.mounted) return;
value++;
}
value++;
});
}
void update(f.BuildContext other, Flag flag) {
if (mounted) setState(() { value++; });
if (other.mounted) this.setState(() { value++; });
if (!mounted) return;
setState(() { value++; });
setState(() {
bool mounted = true;
if (mounted) value++;
if (flag.mounted) value++;
});
void setState(void Function() callback) { callback(); }
setState(() { if (this.mounted) value++; });
}
}
class Overridden extends S {
bool get mounted => true;
void updateFlag() { setState(() { if (mounted) value++; }); }
}
class Fake {
bool mounted = true;
void setState(void Function() callback) { callback(); }
void update() { setState(() { if (mounted) return; }); }
}Configure avoid-mounted-in-setstate
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-mounted-in-setstate": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-mounted-in-setstate: <reason>More correctness rules
- avoid-mutable-equatable-subclass-fieldsrecommendedequatable
Flags mutable instance fields on subclasses inheriting Equatable equality.
- avoid-mutable-global-variables
avoid_mutable_global_variablesDisallow mutable top-level variables.
- avoid-nested-futures
Avoid
Future<Future<T>>,FutureOr<Future<T>>, and other directly nested future wrappers. - avoid-nested-streams-and-futures
Avoid directly nesting SDK streams and futures:
Stream<Future<T>>andFuture<Stream<T>>complicate consumption. - avoid-read-inside-buildrecommendedprovider
Flags non-listening Provider reads directly inside Flutter widget builds.
- avoid-recursive-calls
Reports direct self-calls in functions and methods, including guarded recursion.
- avoid-recursive-widget-callsrecommendedflutter
Warns when a widget's build method constructs the same widget again.
- avoid-ref-inside-state-disposeriverpod
Rejects Riverpod widget references used during State.dispose.