本文介绍了导航器通过pushNamed传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前可能会被问到,但我找不到它,但是如何将参数传递给命名路由呢?

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);
      },
    );

这就是你的称呼Navigator.of(context).pushNamed('home/presentation')

但是如果我的小部件是new Presentation(arg1, arg2, arg3)怎么办?

But what if my widget is new Presentation(arg1, arg2, arg3)?

推荐答案

pushNamed()现在支持合并拉取请求.如果您迫不及待,请切换到频道master(flutter channel master,并可能紧随其后的是flutter upgrade).

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).

如何发送:

    Navigator.pushNamed(ctx, '/foo', arguments: someObject);

如何接收:

...
    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.

这篇关于导航器通过pushNamed传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 20:33