use-setstate-synchronously

Path
lint/correctness/use-setstate-synchronously
Default
On (recommended)
Origin
Falcon
Domain
flutter
Rule
502 of 512
recommendedflutter

Require a mounted check before calling setState after an async gap. Reuses the BuildContext async-state analysis and its control-flow guards.

Invalid

example.dartdart
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++; }); } }
}
Do not call `setState` across an `async` gap without checking `mounted`.
2class S extends State<StatefulWidget> { int x=0;
3Future<void> update0() async { await work(); setState(() { x++; }); }
Do not call `setState` across an `async` gap without checking `mounted`.
3Future<void> update0() async { await work(); setState(() { x++; }); }
4Future<void> update1() async { await work(); this.setState(() { x++; }); }
Do not call `setState` across an `async` gap without checking `mounted`.
4Future<void> update1() async { await work(); this.setState(() { x++; }); }
5Future<void> update2() async { if (mounted) { await work(); setState(() { x++; }); } }
Do not call `setState` across an `async` gap without checking `mounted`.
5Future<void> update2() async { if (mounted) { await work(); setState(() { x++; }); } }
6Future<void> update3() async { await work(); if (!mounted) return; await work(); setState(() { x++; }); }
Do not call `setState` across an `async` gap without checking `mounted`.
6Future<void> update3() async { await work(); if (!mounted) return; await work(); setState(() { x++; }); }
7Future<void> update4() async { try { await work(); } finally { setState(() { x++; }); } }

Valid

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "use-setstate-synchronously": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/use-setstate-synchronously: <reason>