avoid-mounted-in-setstate
Warns when a mounted
check happens inside a setState
callback.
Checking for mounted
inside a setState
call is too late and can lead to an exception. Always check for mounted
before calling setState
.
Example
❌ Bad:
void someMethod() {
setState(() {
// LINT: Avoid checking for 'mounted' inside the 'setState' callback.
// Try moving this check out.
if (context.mounted) {
// ...
}
});
}
✅ Good:
void someMethod() {
if (context.mounted) {
setState(() {
// ...
});
}
}