Why this matters
BuildContext may become invalid if the widget is unmounted during an async operation. Always check mounted before using it.
mounted Before Using BuildContext After an Async OperationVerify if a widget is still mounted before using BuildContext inside an asynchronous operation to prevent accessing invalid states.
BuildContext may become invalid if the widget is unmounted during an async operation. Always check mounted before using it.
Side-by-side examples engineers can pattern-match during review.
@override
Widget build(BuildContext context) => OutlinedButton(
onPressed: () async {
await Future<void>.delayed(const Duration(seconds: 1));
Navigator.of(context).pop(); // Non compliant
},
child: const Text('Delayed pop'),
);@override
Widget build(BuildContext context) => OutlinedButton(
onPressed: () async {
await Future<void>.delayed(const Duration(seconds: 1));
if (context.mounted) {
// The context is mounted, so it's safe to use it
Navigator.of(context).pop();
}
},
child: const Text('Delayed pop'),
);From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.