onGenerateRoute和Flutter中的路由之间的区别 [英] Difference between onGenerateRoute and routes in Flutter

查看:167
本文介绍了onGenerateRoute和Flutter中的路由之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onGenerateRoute 和Flutter中的路由的好处或用例是什么.

What is the benefit or use case of onGenerateRoute and routes in Flutter.

在我的应用程序的 MaterialApp 内的第一页中,我们可以为应用程序定义路由,这与使用 onGenerateRoute 定义的内容相同.

In my application in first page inside MaterialApp we can define routes for our Application the same thing we can define with onGenerateRoute.

两者都用于NamedRoute.

Both are used for NamedRoute.

我怀疑,在哪种情况下我需要使用路由,在哪种情况下我需要使用 onGenerateRoute ?

I have a doubt, in which scenario i need to use routes and in which scenario i need to use onGenerateRoute?

推荐答案

NB(请参见下面的答案):

没有错误,只是问题报告者的用法不好.这是因为他没有将设置对象传递给onGenerateRoute方法返回的新MaterialPageRoute (请参阅问题的最终评论).[...]

There is no bug, just bad usage by the issue reporter. It is caused because he didn't pass the settings object along to the new MaterialPageRoute returned by the onGenerateRoute method (see the issue's final comments). [...]


提及错误"的原始答案:

这两个属性都没有做任何详细说明,但是正如@Alireza指出的那样,首先检查了 routes .

此外,使用 onGenerateRoute 还为您提供了一个在添加新路由(页面)之前添加自定义业务逻辑的地方.例如,如果您要进行一些初始化.

Also, using onGenerateRoute gives you a single place to add your custom business logic before pushing new routes (pages). For example, if you want to do some initializations.

routes属性:

使用Navigator.pushNamed推送命名路由时,路由名称在此地图中向上看.如果存在名称,则关联WidgetBuilder用于构造执行以下操作的MaterialPageRoute包括英雄动画在内的适当过渡路线.

When a named route is pushed with Navigator.pushNamed, the route name is looked up in this map. If the name is present, the associated WidgetBuilder is used to construct a MaterialPageRoute that performs an appropriate transition, including Hero animations, to the new route.

onGenerateRoute属性:

将应用导航到命名的路由时使用的路由生成器回调路线....如果路由不包含请求的路由,则使用此地址.

The route generator callback used when the app is navigated to a named route. ... This is used if routes does not contain the requested route.

重要提示:您真正要意识到的是 onGenerateRoute <中的已知错误./code>属性.

IMPORTANT: What you really want to be aware of is a known bug in onGenerateRoute property.

问题在于,如果您使用 onGenerateRoute 创建命名路由,则将无法从页面中的RouteSettings对象获取该路由的名称.(不过,附加到设置对象的参数很好)换句话说:

The problem is that if you use onGenerateRoute to create named routes you won't be able to get the name of that route from RouteSettings object in your page. (Arguments attached to the settings object are fine though) In other words:

Widget build(BuildContext context) {
    ModalRoute.of(context).settings.name == null;       //bug
    ModalRoute.of(context).settings.arguments != null;  //ok
    ...

如果您想知道当前路线的名称,这可能会影响您.例如,如果您想弹出一些屏幕:

This may affect you in case you would like to know the name of the current route. For example, if you want to pop some screens:

navigator.popUntil(ModalRoute.withName('/login'));

因此,在解决此问题之前,我建议使用 routes:属性.

Therefore, until this problem is resolved, I recommend using the routes: property.

这篇关于onGenerateRoute和Flutter中的路由之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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