Andre Haueisen
Andre Haueisen

Reputation: 518

Detect pushed routes on Flutter

How can I check if a named route is currently on the stack? I do not want to build a new route every time the user pushes a named route that already exists.

Also, is there a way to pop a named route?

Something like Navigator.of(context).popNamed(routeToPop)

Upvotes: 1

Views: 7858

Answers (2)

Andre Haueisen
Andre Haueisen

Reputation: 518

Since in my case I had control over the route instance, I went on to investigate the navigator a little further. I found a method that solved my problem:

navigator.removeRoute(myRoute)

It does not animate the route out though. The route simply vanishes. For what I need, that is ok since when removeRoute is called, the route to be removed is hidden anyway.

Upvotes: 3

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657909

https://docs.flutter.io/flutter/widgets/RouteObserver-class.html allows you to listen to route changes and maintain a copy of the stack yourself where you can look up existing routes.

You can also upvote this issue https://github.com/flutter/flutter/pull/22408 that exposes the history stack.

Upvotes: 3

Related Questions