Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Flutter Navigation stack

Tags:

flutter

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.... }

like image 901
Vlad.d Avatar asked Jan 08 '19 15:01

Vlad.d


People also ask

How do I check my navigation stack in flutter?

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.

What is Pushreplacement in flutter?

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 .


2 Answers

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.

like image 159
Jordan Davies Avatar answered Nov 15 '22 19:11

Jordan Davies


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);
  }
}
like image 28
TheIT Avatar answered Nov 15 '22 19:11

TheIT