October 7, 2022 7:53 pm

How to Pass Arguments to a Named Route in Flutter?

If you looking to implement a pass argument solution in your Flutter application or you are looking to build a custom app for your organization, then consult a Flutter development company like Flutter Agency today.

The Navigator makes it possible to browse to a designated route from any portion of an application by utilizing a common identifier. This capability is provided by Navigator. You could, in some circumstances, also be required to send arguments to a designated route. You could, for instance, want to browse to the /user route and give information on the user to that route.

 

Utilizing the arguments parameter of the Navigator.pushNamed() function will allow you to successfully do this operation. Extract the arguments by using the ModalRoute.of() method or by placing an onGenerateRoute() function in the MaterialApp or CupertinoApp constructor and passing it the relevant parameters.

 

Using the following steps, it explains how to send parameters to a named route and read the arguments using ModalRoute.of() and onGenerateRoute().

 

  • Define the required arguments that you must provide.
  • Make a widget that takes the parameters and save them.
  • Create an entry for the widget in the table of routes.
  • Proceed to the widget by navigating.

STEP 1 – Outline the essential reasons that must be accepted

To begin, you will need to specify the parameters that will be sent to the new route. In this particular illustration, you will send two pieces of data: the message, as well as the title of the screen. Create a class that can keep this information in order to transmit both pieces of data at once. If you looking to implement pass argument solution in your Flutter application or you are looking to build a custom app for your organization, then consult Flutter development company like Flutter Agency today. There are plenty of benefits of Flutter technologies such as high performance, high productivity, and dart language support which ensure the custom application meets all the business requirements.

// You can pass any object to the arguments parameter.

// In this example, create a class that contains both

// a customizable title and message.

class ScreenArguments {

final String title;

final String message;

 

ScreenArguments(this.title, this.message);

}

 

STEP 2- Construct a widget that pulls out the various parameters.

Next, you will need to construct a widget that parses the ScreenArguments in order to get the title and message and then displays them. Utilize the ModalRoute.of() function in order to get access to the ScreenArguments. This function gives you the currently used route along with the passed-in parameters.

// A Widget that extracts the necessary arguments from

// the ModalRoute.

class ExtractArgumentsScreen extends StatelessWidget {

const ExtractArgumentsScreen({super.key});

 

static const routeName = ‘/extractArguments’;

 

@override

Widget build(BuildContext context) {

// Extract the arguments from the current ModalRoute

// settings and cast them as ScreenArguments.

final args = ModalRoute.of(context)!.settings.arguments as ScreenArguments;

 

return Scaffold(

appBar: AppBar(

title: Text(args.title),

),

body: Center(

child: Text(args.message),

),

);

}

}

 

STEP 3- Ensure that the widget is registered in the routes table.

The next step is to include an entry in the routes that are sent to the MaterialApp widget. The names of the routes are used to determine which widget should be produced when the route is followed.

 

MaterialApp(

routes: {

ExtractArgumentsScreen.routeName: (context) =>

const ExtractArgumentsScreen(),

},

)

 

STEP 4- Go to the widget in question.

When the user hits a button using Navigator, you should finally travel to the ExtractArgumentsScreen. Through the use of the arguments property, you may provide the route with the arguments. The title and the message are taken from these arguments and extracted by the ExtractArgumentsScreen.

Check below

// A button that navigates to a named route.

// The named route extracts the arguments

// by itself.

ElevatedButton(

onPressed: () {

// When the user taps the button,

// navigate to a named route and

// provide the arguments as an optional

// parameter.

Navigator.pushNamed(

context,

ExtractArgumentsScreen.routeName,

arguments: ScreenArguments(

‘Extract Arguments Screen’,

‘This message is extracted in the build method.’,

),

);

},

child: const Text(‘Navigate to screen that extracts arguments’),

),

 

You may also get the parameters by using the onGenerateRoute method.

 

You may also extract the parameters directly within an onGenerateRoute() method and then provide them to a widget this way. This is an alternative to simply extracting the arguments inside the widget itself.

 

The onGenerateRoute() method is responsible for generating the appropriate route by using the RouteSettings that are sent into it.

 

MaterialApp(

// Provide a function to handle named routes.

// Use this function to identify the named

// route being pushed, and create the correct

// Screen.

onGenerateRoute: (settings) {

// If you push the PassArguments route

if (settings.name == PassArgumentsScreen.routeName) {

// Cast the arguments to the correct

// type: ScreenArguments.

final args = settings.arguments as ScreenArguments;

 

// Then, extract the required data from

// the arguments and pass the data to the

// correct screen.

return MaterialPageRoute(

builder: (context) {

return PassArgumentsScreen(

title: args.title,

message: args.message,

);

},

);

}

// The code only supports

// PassArgumentsScreen.routeName right now.

// Other values need to be implemented if we

// add them. The assertion here will help remind

// us of that higher up in the call stack, since

// this assertion would otherwise fire somewhere

// in the framework.

assert(false, ‘Need to implement ${settings.name}’);

return null;

},

)

Read more on animixplaynews.com

Leave a Reply

Your email address will not be published.