I want to get a particular named route from the navigation stack and remove it using Navigator.removeRoute(context, route); but how can I get route?
I want to get a particular named route from the navigation stack and remove it using Navigator.removeRoute(context, route); but how can I get route?
This works...
Navigator.removeRoute(context, MaterialPageRoute(builder: (context) => ThePage()));
You have to specify the Route you want to remove not the name
Example
var page2Route = MaterialPageRoute(builder: (context) => Page2());
var page3Route = MaterialPageRoute(builder: (context) => Page3());
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page 1"),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.navigate_next),
onPressed: () {
Navigator.push(
context, page2Route);
},
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page 2"),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.navigate_next),
onPressed: () {
Navigator.push(
context, page3Route);
},
),
);
}
}
class Page3 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page 3"),
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.delete),
label: Text("Remove Page 2"),
onPressed: () {
Navigator.removeRoute(
context, page2Route);
},
),
);
}
}
The output:
You can try this method
Navigator.pushReplacement
Navigator.pushReplacement(context, CupertinoPageRoute(builder: (_) => NewScreen()));
This method does not remove the route. But it does not send the page it belongs to the stack while pushing