Vlad.d
Vlad.d

Reputation: 323

Show Flutter Navigation stack

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

Upvotes: 22

Views: 32541

Answers (5)

navid
navid

Reputation: 1398

Thanks to @TheIT for the idea, I added some considerations to the observer to make it work in my situation.

Side note: You can call isCurrent on the stack items to check for current route.

class CurrentNavigationObserver extends NavigatorObserver {
  final List<Route> _routeStack = [];

  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    _routeStack.add(route);
  }

  @override
  void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
    if (oldRoute == null) {
      if (newRoute != null) {
    // Add the route to the top?
        _routeStack.add(newRoute);
      }
      return;
    }

    // Probably never happens, but anyway..
    if (newRoute == null) {
      _routeStack.remove(oldRoute);
      return;
    }

    if (_routeStack.contains(oldRoute)) {
      _routeStack[_routeStack.indexOf(oldRoute)] = newRoute;
    } else {
      // Add the route to the top?
      _routeStack.add(newRoute);
    }
  }

  @override
  void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
    _routeStack.remove(route);
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    _routeStack.remove(route);
  }
}

Upvotes: 2

Bhargav Sejpal
Bhargav Sejpal

Reputation: 1618

Hack is here ! Add following line to your code temporarily / remove once debug is done

Navigator.of(context).popUntil((route) { print(route.settings.name); return false; });

Upvotes: 5

tyegah123
tyegah123

Reputation: 212

If you're willing to use a library, try Fluro. On the router object, it provides printTree() method to print the navigation tree.

Upvotes: 3

TheIT
TheIT

Reputation: 12219

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);
  }
}

Upvotes: 15

Jordan Davies
Jordan Davies

Reputation: 10881

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.

Upvotes: 17

Related Questions