use-setstate-synchronously
- Group
- Correctness
- Path
lint/correctness/use-setstate-synchronously- Default
- On (recommended)
- Origin
- Falcon
- Domain
- flutter
- Rule
- 502 of 512
Require a mounted check before calling setState after an async gap. Reuses the BuildContext async-state analysis and its control-flow guards.
Invalid
import 'package:flutter/material.dart';
class S extends State<StatefulWidget> { int x=0;
Future<void> update0() async { await work(); setState(() { x++; }); }
Future<void> update1() async { await work(); this.setState(() { x++; }); }
Future<void> update2() async { if (mounted) { await work(); setState(() { x++; }); } }
Future<void> update3() async { await work(); if (!mounted) return; await work(); setState(() { x++; }); }
Future<void> update4() async { try { await work(); } finally { setState(() { x++; }); } }
}class S extends State<StatefulWidget> { int x=0;Future<void> update0() async { await work(); setState(() { x++; }); }Future<void> update0() async { await work(); setState(() { x++; }); }Future<void> update1() async { await work(); this.setState(() { x++; }); }Future<void> update1() async { await work(); this.setState(() { x++; }); }Future<void> update2() async { if (mounted) { await work(); setState(() { x++; }); } }Future<void> update2() async { if (mounted) { await work(); setState(() { x++; }); } }Future<void> update3() async { await work(); if (!mounted) return; await work(); setState(() { x++; }); }Future<void> update3() async { await work(); if (!mounted) return; await work(); setState(() { x++; }); }Future<void> update4() async { try { await work(); } finally { setState(() { x++; }); } }Valid
import 'package:flutter/material.dart';
class S extends State<StatefulWidget> { int x=0;
Future<void> update0() async { await work(); if (!mounted) return; setState(() { x++; }); }
Future<void> update1() async { await work(); if (mounted) { this.setState(() { x++; }); } }
void update2() { setState(() { x++; }); }
Future<void> update3() async { await work(); if (!this.mounted) return; setState(() { x++; }); }
Future<void> update4() async { await work(); if (!context.mounted) return; setState(() { x++; }); }
}Configure use-setstate-synchronously
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"use-setstate-synchronously": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/use-setstate-synchronously: <reason>More correctness rules
- valid-regexps
valid_regexpsrecommendedDisallow statically known invalid
dart:coreregular expressions. - void-checks
void_checksrecommendedDon't pass or assign values to
void(orFutureOr<void>) locations. - add-equatable-propsrecommendedequatable
Flags concrete Equatable subclasses that do not declare an instance props property.
- always-pass-global-keyflutter
Keep
GlobalKeysstable and pass owned State fields to widgets. - always-remove-listenerrecommendedflutter
Requires Flutter Listenable registrations in State to be removed.
- always-use-package-imports
always_use_package_importsDisallow relative imports in libraries under a package's
lib/directory. - avoid-assigning-to-static-field
Flags instance methods writing resolved static fields, including compound assignments and increments.
- avoid-calling-notifier-members-inside-buildrecommendedriverpod
Flags calls to Riverpod notifier methods during widget build.