We all know that The Navigator
has the ability to navigate to a named route from any part of an app. In some cases, what happens is you may also need to pass arguments to a named route. For example, If you want to navigate to the /user/payment
routes and pass information about the user Or payment details to that route.
In Flutter, you can perform this task by providing additional arguments
to the Navigator.pushNamed
method. You can extract the arguments by using the ModalRoute.of
method or inside an onGenerateRoute
function provided to the MaterialApp
or CupertinoApp
constructor.
Passing Arguments To A Named Route In Flutter App:
- Define the arguments you need to pass
- Create a widget that extracts the arguments
- Register the widget in the
routes
table - Navigate to the widget
Define the arguments you need to pass :
First of all, define what arguments you need to pass. In this example, we are passing two pieces of data: one is The title
of the screen and second one is a message
.
For this we need to create a class that stores this information.