-
Notifications
You must be signed in to change notification settings - Fork 28.9k
enable Navigator.of to accept a navigator element and return its stat… #58259
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
Conversation
final NavigatorState navigator = rootNavigator | ||
? context.findRootAncestorStateOfType<NavigatorState>() | ||
: context.findAncestorStateOfType<NavigatorState>(); | ||
// Handles the case where the input context is a navigator element. |
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.
/cc @gspencergoog I seem to remember that you recently also ran into the problem that "self" is not included when you look up ancestors. How did we solve the problem there again?
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.
I created _getAncestor
to fetch the parent so that I can start searching using getElementForInheritedWidgetOfExactType
from the parent element, which avoids returning itself just by starting the search with the parent element.
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.
And my problem was the opposite: getElementForInheritedWidgetOfExactType
can return self, and I didn't want it to.
if (context is StatefulElement) { | ||
if (context.state is NavigatorState) |
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.
I believe these two ifs can be combined into one.
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
…e directly
Description
Navigator.of uses context.findAncestorStateOfType which does not handle the case where the context is a navigator element. this pr fixes it.
Related Issues
Fixes #34105
Tests
I added the following tests:
See files
Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.///
).flutter analyze --flutter-repo
) does not report any problems on my PR.Breaking Change
Did any tests fail when you ran them? Please read Handling breaking changes.