Skip to content

Conversation

@HansMuller
Copy link
Contributor

Replace the current route by pushing a new route and disposing the old one.

Route.didPush() now returns a Future that completes when the push operation completes.

Fixes: #7365
Fixes: #6544

void install(OverlayEntry insertionPoint) { }

/// Called after install() when the route is pushed onto the navigator.
/// Called after install() when the route is pushed onto the navigator. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a paragraph break after the first sentence.

The first line of the dartdoc becomes the summary for the method that is displayed on the class page on the web site.

return navigator.pushNamed(routeName);
}

static Future<dynamic> pushReplacementNamed(BuildContext context, String routeName, { dynamic result }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need docs

assert(newRoute.overlayEntries.isEmpty);

setState(() {
final int index = _history.indexOf(oldRoute);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessarily expensive. We know the old route is the last one in the history, so we can compute this index from the length. We can assert that we got it right with indexOf if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. The indexOf is now just part of an assert as you suggested.


setState(() {
final int index = _history.indexOf(oldRoute);
assert(index >= 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 779, you handle the empty history case but here you assert that we have an old route. We should be consistent about whether we handle that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my mistake on line 779, thanks for catching that.

newRoute.install(_currentOverlayEntry);
_history[index] = newRoute;
newRoute.didPush().then<dynamic>((Null _) {
// The old route's exit is not animated. We're assuming that the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to re-check mounted when this future resolves. Otherwise, we can end up disposing oldRoute twice (once when we unmount and once when the future resolves).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

// The old route's exit is not animated. We're assuming that the
// new route completely obscures the old one.
oldRoute
.._popCompleter.complete(result ?? oldRoute.currentResult)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have docs somewhere that explain all these lifecycle callbacks. It's a bit surprising that we can complete this future without calling didPop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the names of the Route lifecycle callback methods and their roles warrant a little thought.

I've filed an API stability issue: #7617

assert(config.onUnknownRoute != null);
newRoute = config.onUnknownRoute(settings);
assert(newRoute != null);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of this code is repeated with pushNamed. Maybe break out into a common subroutine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I added _routeNamed(String name)

@abarth
Copy link
Contributor

abarth commented Jan 24, 2017

LGTM

@HansMuller HansMuller merged commit 8c5bee9 into flutter:master Jan 24, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigator.replaceNamed Navigator.pushReplacement

3 participants