-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Add BuildContext.mounted #111619
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
Add BuildContext.mounted #111619
Conversation
| /// an asynchronous operation), consider checking [mounted] to determine whether | ||
| /// the context is still valid before interacting with it: | ||
| /// | ||
| /// ```dart |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is almost a functional example app. Would it make sense to make it into a full sample?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, it doesn't demonstrate anything visually though. In this case I think a full sample would actually distract from the point this is trying to make...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, makes sense. I mean it could show a dialog going away after a second, but I agree it's not all that visual.
chunhtai
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
gspencergoog
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not an expert here, but I see State.mounted is sometimes not enough for a State.context to be used.
What I have seen: error, Looking up a deactivated widget's ancestor is unsafe., when using SomeInheritedWidget.of(context) - you know, the very common practice. The error is like
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
When the exception was thrown, this was the stack:
#0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3990:9)
#1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4004:6)
#2 Element.dependOnInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:4019:12)
#3 SomeOfMyInheritedWidget.of (...)
That seems to come from when the widget is deactivated and not yet (re) activated.
My personal workaround is that, has an extra variable tracking activate/deactivate, and never call them when it is deactivated and not yet activated. But that does not sound elegant.
Notice it uses _debugCheckStateIsActiveForAncestorLookup, and your example code, Navigator.of(context), calls findRootAncestorStateOfType which also has the _debugCheckStateIsActiveForAncestorLookup assertion.
Therefore, I suspect this PR will suffer from the same error.
| /// onPressed: () async { | ||
| /// await Future<void>.delayed(const Duration(seconds: 1)); | ||
| /// if (context.mounted) { | ||
| /// Navigator.of(context).pop(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(here)
|
@fzyzcjy Can you file an issue for that? Ideally with some code that reproduces the error you're seeing? I am curious to see how you can do a dependency lookup while a global key move or reparenting is in progress. |
|
@goderbauer Sure I will do that when see the bug again |
|
@goderbauer - is it possible to see which Flutter release this will end up in? I'm getting |
|
I can see this is currently available in the beta branch but not stable as of |
|
BuildContext.mounted does not appear in 3.3.5 |
@ChangJoo-Park I believe we will need to wait for a least the next minor version (>= 3.4.0) |
|
It's been TWO MONTHS after this was merged and still linter warnings are present, linking to the invalid docs page. Why should this wait for the next minor release, shouldn't this have been the immediate patch (bugfix)? This should be considered bugfix, as linter page is a bug (it provides solution that doesn't build). |
|
@Troyx21 This feature is available in the beta and master channels. Please take a look at the description of our release channels to decide if you want to switch to one of those channels, but we typically only release to the stable channel once a quarter or so (every three months), so it is expected for a change to take about that long to get to the stable channel from the time it is committed to master. If you absolutely need the feature sooner than that, you are welcome to switch to a less stable channel in the meantime. |
|
@gspencergoog I do understand that perfectly well. What I tried to communicate, is that this particular issue, because of the broken linter page and annoing linter warning that affects probably thouthands of codebases, could have been identified as a bug, therefore - this PR could have been labeled as a bugfix - and hence merged into stable as soon as the next bugfix (patch) version was issued (3.3.X). |
|
I'm sorry that it's annoying to you, I truly am. However, we are very unlikely to elevate any annoyance issue to the level of a cherrypick/hotfix into the stable channel because the risk to the stability of the stable channel is too high. We set the bar very high for those hotfixes because we want you to have confidence in the stability of the stable channel, as most developers would rather have annoying linter messages than angry customers. As this page mentions, you are also welcome to patch your local version of Flutter if you must be on the stable channel but need the fix sooner. |
|
Is context.mounted already available in version 3.3.10? I am getting the error
@ValentinVignal Is this confirmed anywhere? I am searching for information about the expected release dates & changes of the versions, but I can't find it… 3 months after the merge "seem enough" to release it into the stable version… This is my flutter doctor output: This is my code: It might be the case that I am using it wrong, but if this is not a proper way of loading data from the local storage and saving it to a provider, how should I do it? |
|
@guplem I don't belong to the flutter team so everything I'm saying is not confirmed. It's only suppositions.
Since
Flutter releases a new stable version "roughly once a quarter" , so a new stable version should be released soon (but I don't know when). You can read more about the Flutter build release channels. |
|
Until then, can something like this work? extension BuildContextMountedExt on BuildContext {
bool get mounted {
try {
widget;
return true;
} catch (e) {
return false;
}
}
} |
|
It looks like it has been released with Flutter 3.7.0 🎉 |

This makes it a little easier to use
BuildContexterror-free across asynchronous gaps inStatelessWidgets and other instances where you can't checkState.mounted.Fixes #111488
DO NOT MERGE until
use_build_context_synchronouslylint has been updated to recognizeBuildContext.mounted: dart-lang/sdk#58872