如何在Flutter中将非字符串数据传递到命名路由? [英] How do I pass non-string data to a named route in Flutter?

查看:89
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆