如何将非字符串数据传递给 Flutter 中的命名路由? [英] How do I pass non-string data to a named route in Flutter?
本文介绍了如何将非字符串数据传递给 Flutter 中的命名路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多屏幕,我正在使用 Navigator
.我想使用命名路线",但我还需要将非字符串(例如图像)传递给我的下一条路线.
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.
我不能使用 pushNamed()
因为我不能向它传递非字符串数据.
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?
推荐答案
现在可以将复杂的参数传递给 Navigator.pushNamed
:
It is now possible to pass complex arguments to Navigator.pushNamed
:
String id;
Navigator.pushNamed(context, '/users', arguments: id);
然后可以在 onGenerateRoute
中使用它来使用这些参数自定义路由构建:
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;
}
},
);
这篇关于如何将非字符串数据传递给 Flutter 中的命名路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文