Navigator pass arguments with pushNamed
FlutterFlutter Problem Overview
Might have been asked before but I can't find it but how do you pass a arguments to a named route?
This is how I build my routes
Widget build(BuildContext context) {
return new Navigator(
initialRoute: 'home/chooseroom',
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
switch (settings.name) {
case 'home/chooseroom':
// navigates to 'signup/choose_credentials'.
builder = (BuildContext _) => new ChoosePage();
break;
case 'home/createpage':
builder = (BuildContext _) => new CreateRoomPage();
break;
case 'home/presentation':
builder = (BuildContext _) => new Presentation();
break;
default:
throw new Exception('Invalid route: ${settings.name}');
}
return new MaterialPageRoute(builder: builder, settings: settings);
},
);
This is how you call it
Navigator.of(context).pushNamed('home/presentation')
But what if my widget is new Presentation(arg1, arg2, arg3)
?
Flutter Solutions
Solution 1 - Flutter
No need for onGenerateRoute
. Simply use
var exampleArgument = 'example string';
Navigator.pushNamed(
context,
'/otherscreen',
arguments: {'exampleArgument': exampleArgument},
);
and extract the arguments as follows:
@override
Widget build(BuildContext context) {
final arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
print(arguments['exampleArgument']);
return Scaffold(...);
}
Solution 2 - Flutter
pushNamed()
now supports arguments as of this merged pull request. If you can't wait, switch to channel master
(flutter channel master
and probably followed by flutter upgrade
).
How to send:
Navigator.pushNamed(ctx, '/foo', arguments: someObject);
How to receive:
...
return MaterialApp(
...
onGenerateRoute: _getRoute,
...
);
...
Route<dynamic> _getRoute(RouteSettings settings) {
if (settings.name == '/foo') {
// FooRoute constructor expects SomeObject
return _buildRoute(settings, new FooRoute(settings.arguments));
}
return null;
}
MaterialPageRoute _buildRoute(RouteSettings settings, Widget builder) {
return new MaterialPageRoute(
settings: settings,
builder: (ctx) => builder,
);
}
The "arguments" can be any object, e.g. a map.
Solution 3 - Flutter
It took me a while to notice this, as I'm a newbie to Flutter. But the arguments you add using Navigator.pushNamed
get sent directly to the widget you pushed NOT the MaterialApp for routing.
So in widget you push a new screen from you'll have:
Navigator.pushNamed(
context,
SomePage.routeName,
arguments: {
'v1': 'data1',
'v2': 'data2',
'v3': 'data3',
},
)
You won't need those arguments in your constructor at all. Instead your pull them out in the SomePage widget like the others are saying; namely via:
final arg = ModalRoute.of(context)!.settings.arguments as Map;
and can assign them within SomePage build like:
randomVar1 = arg['v1'];
randomVar2 = arg['v2'];
randomVar3 = arg['v3'];
using whatever keys you put in.
if you want MaterialApp to handle it then you use the onGenerateRoute method. This took me forever to notice that the arguments go directly to the widget pushed. For me it was counter-intuitive.
Solution 4 - Flutter
Arguments can be any object, you can make an array as you can see:
Navigator.of(context).pushNamed('/upload', arguments: {'_imagePath':_imagePath,
'num_docfiscal':num_docfiscal,'dta_docfiscal':dta_docfiscal});
and access to the router class.
Solution 5 - Flutter
Basically you have 2 options :
- Use some 3rd party package for routing - I think the best is Fluro .
- exploit
onGenerateRoute
. This option is limited to args you can pass (string/numbers)
To use second option, assuming you want to pass three arguments: Navigator.of(context).pushNamed('home/presentation:arg1:1337:hello')
MaterialApp (
... ,
onGenerateRoute: handleRoute,
routes:... , )
Route<dynamic> handleRoute(RouteSettings settings) {
WidgetBuilder builder;
final List<String> uri = settings.name.split('/');
if (uri[0].startsWith('home')) {
// handle all home routes:
if(uri[1].startsWith('presentation:'){
// cut slice by slice
final String allArgs =
uri[1].substring('presentation:'.length);
final List<String> args = allArgs.split(':');
// use your string args
print(args[0]); // prints "arg1"
int x = int.parse(args[1]); // becomes 1337
print(args[2]); // prints "hello"
builder = (ctx)=> Presentation(args[0],args[1],args[2]);
...
Solution 6 - Flutter
For simple navigation with arguments from WidgetA to WidgetB
- Define routes in MaterialApp main widget:
return MaterialApp(
routes: {
'/routeAB': (context) => WidgetB(),
},
- In WidgetA use pushNamed method to navigate to WidgetB:
onTap: () {
Navigator.pushNamed(context, '/routeAB',
arguments: {
'arg1': val1,
'arg2': val2,
...
- Get arguments in WidgetB:
Map args = ModalRoute.of(context).settings.arguments;
Solution 7 - Flutter
Pass arguments:
Navigator.pushNamed(YourScreen.routeName, arguments: {"title":myTitle, "user_name":userName});
Extract arguments:
Map<String, dynamic> arguments = new Map<String, dynamic>.from(settings.arguments);
page = MyRecordingScreen(title: arguments["title"], tags: arguments["user_name"], );
Solution 8 - Flutter
For named Routes
with multiple arguments or dynamic object you need to do as follow(this is MVVM pattern example):
navigator:
void navigateEditParty(int index) {
_navigationService.navigateTo(PartyEditRoute,
arguments: {"hunter": hunter, "index": index});
}
router:
case PartyEditRoute:
Map args = settings.arguments;
return _getPageRoute(
routeName: settings.name,
viewToShow: PartyEditView(
hunter: args["hunter"],
index: args["index"],
),
);
class:
PartyEditView({Key key, this.hunter, this.index}) : super(key: key);
Solution 9 - Flutter
It is always best to wrap your arguments in an object to avoid error prone code.
Below are the working example on how you can achieve it in flutter dart.
To send
Navigator.of(context).pushNamed(Routes.submitSuccess, arguments: successModel);
To Receive
case Routes.submitSuccess:
if (settings.arguments is SubmitSuccessModel) {//Check for instance here
return CupertinoPageRoute(
builder: (_) =>
SubmitSuccessPage(successModel: settings.arguments));
}
Model Object
class SubmitSuccessModel {
SubmitSuccessModel(
{this.title, this.desc, this.actionButtonName, this.widget});
String title;
String desc;
String actionButtonName;
Widget widget;
}
final SubmitSuccessModel successModel = SubmitSuccessModel(
title: 'Title',
desc: 'Desc',
actionButtonName: 'Done',
);
Solution 10 - Flutter
Note: new routing options may be available soon.
Could this be a possible option?
>Use push
with a RouteSettings
argument specifying the named route.
This way you can directly pass arguments of any type (including objects) to your destination Widget in a type safe manner and skip using arguments
. You won't need to create a single-use throwaway arguments class nor a Map.
RouteSettings
in push
can supply a named Route
to your Navigation stack which you can search for / use in future routing decisions, just the same as if you had used pushNamed
.
Push + RouteSettings
To use push
with a named route, use RouteSettings
argument with the route name.
Example: a user logs in on Page1
and now you want push
them from Page1
to Page2
Directly inside Page1
pass the User
object (loggedInUser
) to Page2
within a Navigator.push
call and use a RouteSettings
arg with your route name (/page2
).
Navigator.push(context, MaterialPageRoute(
builder: (context) => Page2(user: loggedInUser),
settings: RouteSettings(name: '/page2')
));
And in Page2 widget, you can expect and use the User
object directly.
class Page2 extends StatefulWidget {
final User loggedInUser;
Page2(this.loggedInUser);
@override
_Page2State createState() => _Page2State();
}
class _Page2State extends State<Page2> {
User loggedInUser;
@override
void initState() {
super.initState();
loggedInUser = widget.loggedInUser;
print(loggedInUser.name);
}
@override
Widget build(BuildContext context) {
}
}
Later on you can use /page2
route name. For example if you're at /page3
and you want to popUntil(context, ModalRoute.withName('/page2'))
, this allows that.
Under the Hood
Flutter's Navigator
class shows pushNamed
uses push
+ routeNamed
and routeNamed
uses RouteSettings
.
Future<T> pushNamed<T extends Object>(
String routeName, {
Object arguments,
}) {
return push<T>(_routeNamed<T>(routeName, arguments: arguments));
}
Route<T> _routeNamed<T>(String name, { @required Object arguments, bool allowNull = false }) {
final RouteSettings settings = RouteSettings(
name: name,
arguments: arguments,
);
Route<T> route = widget.onGenerateRoute(settings) as Route<T>;
return route;
Solution 11 - Flutter
It took me a while to notice this, as I'm a newbie to Flutter. But the arguments you add using Navigator.pushNamed
get sent directly to the widget you pushed NOT the MaterialApp for routing.
You don't need those arguments in your constructor at all. Instead your pull them out in the widget like the others are saying; namely via:
final arg = ModalRoute.of(context)!.settings.arguments as Map;
and can assign them like:
randomVar1 = arg['v1'];
randomVar2 = arg['v2'];
randomVar3 = arg['v3'];
using whatever keys you put in.
if you want MaterialApp to handle it then you use the onGenerateRoute method. This took me forever to notice that the arguments go directly to the widget pushed. For me it was counter-intuitive.
Solution 12 - Flutter
screeen_1.dart
Navigator.of(context).pushNamed('/screen2',arguments: {'var1': val1 ,'var2': val2, "var3": val3 ,"var4" : val4}); //sending of the values to route_generator.dart
route_generator.dart
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) //from screeen_1.dart
{
// Getting arguments passed in while calling Navigator.pushNamed
Map args = settings.arguments;
switch (settings.name) {
case '/screen2':
if (args is Map) {
return MaterialPageRoute(
builder: (_) => screen_2(args),
);
}
return _errorRoute(); //optional written in a scaffold when any error arrises
default:
return _errorRoute(); //optional written in a scaffold when any error arrises
}
screen_2.dart
screen_2(Map args) //follow the params from the route_generator.dart
{
return Scaffold(
body: FutureBuilder(
future: _screen_2EndpointProvider.fetchScreen_2(args['var1'], args["var2"], args['var3'], args['var4']), //API call inside which it called the args
builder: (context, snapshot) {
return snapshot.hasData
? screen_2(param: snapshot.data)
: Center(child: CircularProgressIndicator());
}),
);
}
I tried to provide a simple example, if it could be helpful to anyone, because i was facing the same error after a lots of trials it got solved.
Solution 13 - Flutter
// Main Screen from where we want to Navigate
Navigator.pushNamed(
context,
"/ScreenName",
arguments: {
'id': "20"
});
I also faced the same issue I was using NavigationRoutes
class NavigationRoutes {
static Route<dynamic> generateScreenRoute(RouteSettings settings) {
// TODO below we are able to get Arguments list
Map args = settings.arguments;
switch (settings.name) {
case "Screen Name":
return MaterialPageRoute(builder: (context) => ScreenName(args));
// TODO pass above argument here and get in the form of Constructor in Screen
// Name Class
}
}