I want to navigate to next screen then do not back to any screen, but when I use Navigator.pushReplacement my app has back button. I mean, arrow back key still work. I want to go to next screen totally, and no back.
Hope anyone help me.
This is my code
return BlocProvider<PostBloc>(
create: (BuildContext context) => postBloc..add(CreatePostStartEvent()),
child: Scaffold(
body: BlocConsumer<PostBloc, PostState>(
listener: (context, state) async {
if (state is PostCreationSuccessState) {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => const Posts()));
}
},
...
2
Answers
This method pops the current route and pushes a new named route.
If you need to clear all screens in the navigation stack and push a new one in Flutter, you can use the
Navigator.of(context, rootNavigator: true).pushNamedAndRemoveUntil
method. This will remove all existing routes and push the specified route as the new one.Navigator.of(context, rootNavigator: true)
: This accesses the navigator of the root context..pushNamedAndRemoveUntil('/home', (Route<dynamic> route) => false)
: This method removes all the routes until the given predicate returns true. The predicate(Route<dynamic> route) => false
always returns false, ensuring that all routes are removed. Then, it pushes the new route specified by the name ‘/home’.This approach is useful when you want to reset the navigation stack and start a fresh with a new screen.