-
Notifications
You must be signed in to change notification settings - Fork 1.7k
use_build_context_synchronously is triggered when using context.mounted #59009
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
use_build_context_synchronously is triggered when using context.mounted #59009
Comments
If it helps here is an example failing test:
The added second
|
This bug is pretty anoying. Hope you can fix this soon :) |
For fixing this, should we search for specific statements, e.g. |
It's unfortunate that we're approximating what flow analysis does more completely but I'm not aware of any way to leverage it in a lint implementation (though it has been discussed). /fyi @stereotype441 |
Just ran into this issue today, pretty unintuitive why if (!context.mounted) return; was failing but if (context.mounted) doSomething(context) wasn't |
@Levi-Lesches While this is not fixed, if you really want to return instead of placing more code inside an if, you can use: if (context.mounted) {
} else {
return;
} It's not great, but sometimes it can be better than nesting ifs, plus when this is fixed the refactoring will be pretty quick. |
Cool tip, @luccasclezar! And to be able fo find these instances later, I modified it to if (context.mounted) {
// FIXME
} else {
return;
} |
I hope it will be corrected. :) |
I'm continuing to see this issue as of a Dart SDK version from today (from the Flutter main/master channel):
In particular, if I take a test case from the PR dart-archive/linter#4312: import 'package:flutter/widgets.dart';
void foo(BuildContext context) async {
await f();
if (!context.mounted) {
return;
}
Navigator.of(context);
}
Future<void> f() async {} and run
Is that unexpected, or is there something I'm missing? |
Dart SDK at head is at linter e36813b which does not include this fix. |
Good to know, thanks. What's the typical cadence of when new linter versions enter the Dart SDK? The readme for this repo just says:
It's perfectly reasonable that it doesn't propagate instantly; I'm just curious what to expect. |
Relatedly: is there a straightforward way to look up what linter version is in the SDK? It'd be great to document that in the readme file too, if possible. |
All good questions! I'd say we're aspiring for continuous deployment; each commit here should land ASAP in the Dart SDK. But we're definitely not there yet, commits here typically make it over to the Dart SDK in under a month.
We use a homegrown vendoring system, so you'll see the commit SHA in this file: https://github.com/dart-lang/sdk/blob/main/DEPS#L147 |
This workaround recently stopped working: this inverted form of early return no longer persuades the lint rule not to complain when we go on to use `context`. Not sure exactly what change caused that; it happened with a new Flutter version from master/main in recent weeks, presumably due to a new Dart SDK version rolling through there. Since the workaround is no longer effective, just switch to the straightforward early return, and use a lint-ignore comment pointing at the underlying issue that causes the lint rule to complain there: https://github.com/dart-lang/linter/issues/4007 That issue was recently fixed in the linter repo's main branch. The fix rolled yesterday into the Dart SDK: dart-lang/sdk@f09ce66 but that hasn't quite yet rolled through to Flutter. Once it does, we'll be able to cut these.
This includes a workaround for mobx stores: mobxjs/mobx.dart#809 Also, while uuid was already imported in fullscreenable_image.dart, it was not in pubspec.yaml. Lastly, there are some linting issues with use_build_context_synchronously. For the files most heavily impacted, I have added a temporary ignore rule as the linting fixes are not currently in the Flutter SDK. See: https://github.com/dart-lang/linter/issues/4007
This includes a workaround for mobx stores: mobxjs/mobx.dart#809 Also, while uuid was already imported in fullscreenable_image.dart, it was not in pubspec.yaml. Lastly, there are some linting issues with use_build_context_synchronously. For the files most heavily impacted, I have added a temporary ignore rule as the linting fixes are not currently in the Flutter SDK. See: https://github.com/dart-lang/linter/issues/4007
Adds flutter_lints, removes duplicate rules, and addresses linting issues. See #232 for motivation. This includes a [workaround](mobxjs/mobx.dart#809) for mobx stores. Also, while the uuid package was already imported in fullscreenable_image.dart, it was not in pubspec.yaml. Lastly, there are some issues with the use_build_context_synchronously rule. For the files most heavily impacted, I have added a temporary ignore rule as the linting fixes are not currently in the Flutter SDK. See: https://github.com/dart-lang/linter/issues/4007
This includes a workaround for mobx stores: mobxjs/mobx.dart#809 Also, while uuid was already imported in fullscreenable_image.dart, it was not in pubspec.yaml. Lastly, there are some linting issues with use_build_context_synchronously. For the files most heavily impacted, I have added a temporary ignore rule as the linting fixes are not currently in the Flutter SDK. See: https://github.com/dart-lang/linter/issues/4007
This includes a workaround for mobx stores: mobxjs/mobx.dart#809 Also, while uuid was already imported in fullscreenable_image.dart, it was not in pubspec.yaml. Lastly, there are some linting issues with use_build_context_synchronously. For the files most heavily impacted, I have added a temporary ignore rule as the linting fixes are not currently in the Flutter SDK. See: https://github.com/dart-lang/linter/issues/4007
After upgrading to Flutter 3.7, using BuildContext.mounted in any way other than
if (context.mounted) {}
makes the linter complain about using context across async gaps.@goderbauer told me to mention him.
Reproduction steps
flutter create
main.dart
!context.mounted
is just one example of this problem. This is reproducible when using ternary operators or initializing a variable tocontext.mounted
too.The text was updated successfully, but these errors were encountered: