在颤动的应用程序屏幕之间传递数据或参数的最佳方法是什么? [英] What is the best way to pass data or arguments between flutter app screens?

查看:115
本文介绍了在颤动的应用程序屏幕之间传递数据或参数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将参数从一个屏幕发送到另一个屏幕,我必须将此参数设置为widgets类中的字段.在将许多参数传递给几个小部件的情况下,我认为这可能会给大型应用程序带来问题.

在屏幕之间有效传输数据的最佳方法是什么?

解决方案

通常最好的方法是使用应用程序状态管理.您在切换路线之前编辑状态,并在新屏幕上读取状态.如果坚持不懈,这样做的好处还在于能够在关闭后将您的应用还原到最后一个状态(这在手机上经常发生).

根据应用程序状态的复杂程度,您可以使用内置的状态管理flutter或redux之类的插件.

Redux需要更多样板代码,但也提供了更大的灵活性,并且对于经常需要的超大型应用程序而言.

以下是有关该主题的一些有趣文章.

用于状态管理的官方文档

是否为Redux

请参见这个答案).

还有一个使传递参数变得容易的软件包:https://pub.dartlang.org/packages/navigate.

带有导航包的示例如下(摘自 github问题):

  Map arg =  {"transactionType":TransactionType.fromLeft,"replaceRoute":ReplaceRoute.thisOne};
    Navigate.navigate(context,
                          "home",
                          transactionType:TransactionType.fromLeft ,
                          replaceRoute: ReplaceRoute.thisOne,
                          arg: arg
                          );

In order to send an argument from one screen to the other I have to set this argument as a field in the widgets class. With many arguments being passed down to several widgets I think this might cause a problem for bigger applications.

What is the best way to pass data between screens efficiently?

解决方案

Typically the best way is to use app state management. You edit the state before switching routes and read the state on the new screen. If done with persistence this also has the benefit of being able to restore your app to the last state after being closed (which happens frequently on phones).

Depending on the complexity of your app state you could use the flutter built in state management or an addon like redux.

Redux requires more boilerplate coding but also offers more flexibility and for very large apps that is often required.

Here are a few interesting articles about the subject.

Official docs for state management

Redux or not

Help with choosing state management

Alternatives are to pass arguments on route navigation. This can be done either as part of the route string (which doesn't work with static route strings) or with a MaterialPageRoute for example (see this answer).

There is also package that makes passing parameters easy: https://pub.dartlang.org/packages/navigate.

An example with the navigate package looks like this (taken from this github issue):

  Map arg =  {"transactionType":TransactionType.fromLeft,"replaceRoute":ReplaceRoute.thisOne};
    Navigate.navigate(context,
                          "home",
                          transactionType:TransactionType.fromLeft ,
                          replaceRoute: ReplaceRoute.thisOne,
                          arg: arg
                          );

这篇关于在颤动的应用程序屏幕之间传递数据或参数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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