0

I have looked it up, but how can I navigate to the root using the GoRouter package? Currently, I am using context.pop() corresponding to each current path, like this:

context.pop();
context.pop();
context.pop();

Here is how I have defined the GoRouter: [Please provide the GoRouter definition here.


class RouterService {
  final router = GoRouter(
    initialLocation: '/',
    routes: [
      GoRoute(
        name: 'rootPage',
        path: '/',
        pageBuilder: (BuildContext context, GoRouterState state) {
          return MaterialPage(
            key: state.pageKey,
            child: RootPage(),
          );
        },
      ),
    ],
  );
}
sub
  • 117
  • 1
  • 6

1 Answers1

0

GoRouter provides a canPop method. From the documentation:

Returns true if there is at least two or more route can be pop.

You can use it to pop your pages until there is 2 pages : the root, and the last page to pop.

void popUntilRoot() {
  while (goRouter.canPop()) {
    goRouter.pop();
  }
}
lucie
  • 895
  • 5
  • 19