diff --git a/pkg/linter/lib/src/rules/use_build_context_synchronously.dart b/pkg/linter/lib/src/rules/use_build_context_synchronously.dart index 663b6b5c25fd..1126929dcee1 100644 --- a/pkg/linter/lib/src/rules/use_build_context_synchronously.dart +++ b/pkg/linter/lib/src/rules/use_build_context_synchronously.dart @@ -270,12 +270,14 @@ class AsyncStateVisitor extends SimpleAstVisitor { return switch ((leftGuardState, rightGuardState)) { // Anything on the left followed by async on the right is async. (_, AsyncState.asynchronous) => AsyncState.asynchronous, - // Async on the left followed by anything on the right is async. + // Anything on the left followed by not-mounted on the right is a + // not-mounted check. + (_, AsyncState.notMountedCheck) => AsyncState.notMountedCheck, + // Async on the left followed by anything else on the right is async. (AsyncState.asynchronous, _) => AsyncState.asynchronous, // A mounted guard only applies if both sides are guarded. (AsyncState.mountedCheck, AsyncState.mountedCheck) => AsyncState.mountedCheck, - (_, AsyncState.notMountedCheck) => AsyncState.notMountedCheck, (AsyncState.notMountedCheck, _) => AsyncState.notMountedCheck, // Otherwise it's just uninteresting. (_, _) => null, diff --git a/pkg/linter/test/rules/use_build_context_synchronously_test.dart b/pkg/linter/test/rules/use_build_context_synchronously_test.dart index accd7a1ca96d..8c3e1c72d633 100644 --- a/pkg/linter/test/rules/use_build_context_synchronously_test.dart +++ b/pkg/linter/test/rules/use_build_context_synchronously_test.dart @@ -705,6 +705,22 @@ void foo(BuildContext context) async { AsyncState.asynchronous); } + test_ifStatement_referenceAfter_asyncOrNotMountedInCondition() async { + await resolveCode(r''' +import 'package:flutter/widgets.dart'; +void foo(BuildContext context) async { + if (await Future.value(true) || !context.mounted) return; + //} else { + context /* ref */; + //} +} +'''); + var body = findNode.ifStatement('if ').parent!; + var reference = findNode.statement('context /* ref */'); + expect(body.asyncStateFor(reference, contextElement), + AsyncState.notMountedCheck); + } + test_ifStatement_referenceAfter_awaitThenExitInElse() async { await resolveCode(r''' import 'package:flutter/widgets.dart';