I want to see the navigation stack in Flutter. Is it possible to use the Navigator
class to print the navigation stack and other meta info about what's inside the Navigator?
Printing the Navigator
with the print()
method just gives the Navigator
as a string.
Expected result: Navigator : { params...., params.... }
The Navigator class provides all the navigation capabilities in a Flutter app. Navigator provides methods to mutate the stack by a push to stack or by popping from the stack. The Navigator. push method is for navigating to a newer page and Navigator.
Replace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in. If non-null, result will be used as the result of the route that is removed; the future that had been returned from pushing that old route will complete with result .
I think the only way you could do this currently is to override the Navigator
class and keep track of the Routes yourself.
If you look at the Navigator source code there is a variable called _history
which contains all the navigated routes but there's no way to access it unfortunately.
Perhaps the Navigator
's observers
parameter could help you? Though it would involve manually keeping track of the internal stack state of the Navigator
. You could then operate on the routeStack
member as necessary.
...
Navigator(
observers: [MyNavigatorObserver()],
onGenerateRoute: ...,
)
...
class MyNavigatorObserver extends NavigatorObserver {
List<Route<dynamic>> routeStack = List();
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
routeStack.add(route);
}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
routeStack.removeLast();
}
@override
void didRemove(Route route, Route previousRoute) {
routeStack.removeLast();
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
routeStack.removeLast();
routeStack.add(newRoute);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With