-
Notifications
You must be signed in to change notification settings - Fork 28.6k
context.mounted gives Don't use 'BuildContext's across async gaps
message.
#116779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report @RoarGronmo |
I think it hasn't any practical issue, other that Lint is flagging it as "suspicius", when it wasn't "suspicius" in versions prior to 3.7.x. But if it is "suspicius" after 3.7, how can we test the availability of context, when we cannot use |
BTW: As you see from the code suggestion which Flutter gives above, method is to use |
Thanks for the update. Keeping this issue open and labeling for team's attention and insights. |
/cc @goderbauer |
@RoarGronmo Could you please post some real example code that reproduces this warning? I tried to reproduce it with the following code, but it was behaving as expected: import 'package:flutter/material.dart';
Future<void> foo(BuildContext context) async {
await Future.delayed(const Duration(seconds: 1));
Material.of(context); // produces warning, as expected.
if (context.mounted) { // no warning, as expected.
Material.of(context); // no warning, as expected.
}
} |
Well, posting 550 lines not so suitable but I will give some more basic info first, that might give you some clues:
pubspec.yaml
A simple timeline scheme (extraction):
You will certainly not let it work since you miss some internal libraries, but the clue is: Note: the |
Addendum: Below, I tried to add your code into the class where this problem appears, and sees that your example appears correctly, and it simultaniously "lints" the usage of I think you need to get som oxygen and dive deeper into this behavior ;) |
@RoarGronmo Can you reduce the code sample that shows the erroneous behavior by removing subsequently removing all the stuff that isn't relevant? That will make it easier to debug this. |
I have reworked the standard example to launch an AlertDialog with two buttons to mimick a delay. As far as I see, the clue is that
I hope this give you a clue ! -- Merry X-mas -- |
After upgrading to Flutter 3.7, I'm experiencing the same issue in VS Code. Besides the ternary operator, using It's pretty simple to reproduce:
class AsyncContextTestWidget extends StatelessWidget {
const AsyncContextTestWidget({super.key});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () async {
await Future<void>.delayed(Duration.zero);
if (!context.mounted) {
return;
}
},
child: const Text('Test'),
);
}
} |
@luccasclezar Could you file an issue with that repo case at https://github.com/dart-lang/linter/ and cc me please? It looks like the lint has a bug. |
@goderbauer Done! 🙂 |
Closing this here since we are tracking the problem now at dart-lang/sdk#59009. |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
After upgrading to Flutter
3.7.0-1.1.pre
on channel beta thecontext.mounted
is flaggedin use after async gap
I thought in my simple mind that
context.mounted
was "allowed" to be in use after async gap to detect valid context. If this should be so, what should we use instead to detect context validity ?The text was updated successfully, but these errors were encountered: