Skip to content

Commit 725f4a6

Browse files
authored
Do not trigger use_build_context_synchronously on context.mounted (dart-archive/linter#4134)
1 parent 01fb176 commit 725f4a6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/src/rules/use_build_context_synchronously.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ class _Visitor extends SimpleAstVisitor {
418418
}
419419
// Getter access.
420420
if (isBuildContext(node.prefix.staticType, skipNullable: true)) {
421-
check(node);
421+
if (node.identifier.name != 'mounted') {
422+
check(node);
423+
}
422424
}
423425
}
424426
}

test_data/rules/use_build_context_synchronously.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ class _MyState extends State<MyWidget> {
282282
Navigator.of(context).pushNamed('routeName'); // OK
283283
}
284284

285+
void methodWithBuildContextParameter4(BuildContext context) async {
286+
await Future<void>.delayed(Duration());
287+
if (!context.mounted) return;
288+
await Navigator.of(context).pushNamed('routeName'); // OK
289+
}
290+
285291
void methodWithMountedFieldCheck(
286292
BuildContext context, WidgetStateContext stateContext) async {
287293
await Future<void>.delayed(Duration());

0 commit comments

Comments
 (0)