Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass non-string data to a named route in Flutter?

Tags:

flutter

dart

I have many screens, and I'm using the Navigator. I'd like to use "named routes", but I also need to pass non-string (such as images) to my next route.

I can't use pushNamed() because I can't pass non-string data to it.

How can I use a named route + send non-string data?

like image 898
Seth Ladd Avatar asked Nov 21 '17 18:11

Seth Ladd


People also ask

Can Flutter routes be named?

The solution is to define a named route, and use the named route for navigation. To work with named routes, use the Navigator.


1 Answers

EDIT:

It is now possible to pass complex arguments to Navigator.pushNamed:

String id; Navigator.pushNamed(context, '/users', arguments: id); 

It can then be used within onGenerateRoute to customize route building with these arguments:

MaterialApp(   title: 'Flutter Hooks Gallery',   onGenerateRoute: (settings) {     final arguments = settings.arguments;     switch (settings.name) {       case '/users':         if (arguments is String) {           // the details page for one specific user           return UserDetails(arguments);         }         else {           // a route showing the list of all users           return UserList();         }       default:         return null;     }   }, ); 
like image 85
Rémi Rousselet Avatar answered Sep 23 '22 21:09

Rémi Rousselet