问题描述
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(请参见下面的答案):
提及错误"的原始答案:
这两个属性都没有做任何详细说明,但是正如@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.
重要提示:您真正要意识到的是 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中的路由之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!