Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Do not trigger use_build_context_synchronously on context.mounted #4134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/src/rules/use_build_context_synchronously.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ class _Visitor extends SimpleAstVisitor {
visitPrefixedIdentifier(PrefixedIdentifier node) {
// Getter access.
if (isBuildContext(node.prefix.staticType, skipNullable: true)) {
check(node);
if (node.identifier.name != 'mounted') {
check(node);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test_data/rules/use_build_context_synchronously.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ class _MyState extends State<MyWidget> {
Navigator.of(context).pushNamed('routeName'); // OK
}

void methodWithBuildContextParameter4(BuildContext context) async {
await Future<void>.delayed(Duration());
if (!context.mounted) return;
await Navigator.of(context).pushNamed('routeName'); // OK
}

void methodWithMountedFieldCheck(
BuildContext context, WidgetStateContext stateContext) async {
await Future<void>.delayed(Duration());
Expand Down