Flutter remove specific route from route stack

Viewed 21

I want to remove a specific route and stay on the same page without navigating to different page.

I have three pages PageA, PageB, PageC. I have the below code in PageC and removing PageB from Route stack. After removing it's navigating back to PageA (default route page). I want to stay on the RouteC without navigating.

final newRouteName = "/PageB";

navigatorKey.currentState!.popUntil((route) {
  if (widget.routeSettings!.name == newRouteName) {
    navigatorKey.currentState!.removeRoute(route);
  }
  return true;
});

Please help me, i stuck with this for days

1 Answers

I dont know if this works for you, but probably what you can do is: When navigating from 'Page B' to 'Page C' make a

void _moveToPageC() {
  Navigator.pushReplacementNamed(context, '/routeC');
}

That way, your stack would be page a > page c Then if you want to go back to page a then you can simply pop page C Hope, this helps

Related