在Flutter中的小部件之间传递数据的最佳方法 [英] The best way to passing data between widgets in Flutter

查看:123
本文介绍了在Flutter中的小部件之间传递数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了自己的自定义窗口小部件,该窗口小部件用于其他视图。在此自定义窗口小部件中,我有一个存储信息的类属性,假设这是一个在窗口小部件内获取新项目的列表。现在,我想从我的主窗口小部件级别的列表中获取项目。

I develop my own custom widget which is used in some other view. In this custom widget, I have one class property which stores information, let's say that this is a list which gains new items inside the widget. Now I want to get items from this list from the level of my main widget.

该怎么做?
我不想创建这样的变量: var customWidget = MyCustomWidget()然后获取内部变量,例如 customWidget .createState()。myList -我认为这是一个糟糕的解决方案(而且我不确定是否可以使用)。将列表传递给自定义窗口小部件的构造函数看起来也很丑陋。

How to do that? I don't want to create a variable like this: var customWidget = MyCustomWidget() and then, get the inside variable like customWidget.createState().myList - I think this is a terrible solution (and I'm not sure if it will work). Also passing a list to the constructor of my custom widget looks very ugly.

还有其他方法来获取其他窗口小部件的状态吗?

Is there any other way to get other widget's state?

推荐答案

首先,请记住,在Flutter中,数据只能向下传递。从设计上说,不可能访问孩子的数据,只能访问父母的数据(尽管周围有一些骇客)。

First, bear in mind that in Flutter data can only be passed downward. It is by design not possible to access data of children, only parents (although there are some hacks around it).

从这一点出发,有两种主要的解决方案可以通过数据:

From this point, there are 2 main solutions to pass data:


  1. 将所需数据添加到小部件构造函数中。

我认为这里没有太多要说的。易于使用。但是当您想将一个字段传递给所有小部件树时很无聊。仅当值的范围受到限制时才使用此方法。如果是配置或用户详细信息之类的,请使用第二种解决方案。

I don't think there is much to say here. Easy to use. But boring when you want to pass one field to all your widget tree. Use this method only when the scope of a value is limited. If it's something like configurations or user details, go for the second solution.

class Bar extends StatelessWidget {
  final String data;

  Bar({this.data});

  @override
  Widget build(BuildContext context) {
    return Text(data);
  }
}




  1. 使用Flutter的 BuildContext

  1. Using Flutter's BuildContext

每个小部件都可以访问 BuildContext 。此类允许一个窗口小部件使用以下一种方法从其任何祖先获取信息:

Each widget has access to a BuildContext. This class allows one widget to fetch information from any of their ancestors using one of the following methods:

ancestorWidgetOfExactType(Type)

...

事实上,如果有数据需要多次访问,更喜欢 inheritFromWidgetOfExactType

As a matter of facts, if there's a data that needs to be accessed many times, prefer inheritFromWidgetOfExactType.

此方法使用 InheritedWidget ;

This uses InheritedWidget; which are specifics kind of widgets that are extremely fast to access to.

请参见 Flutter:如何正确使用继承的小部件?以了解有关其用法的更多详情

See Flutter: How to correctly use an Inherited Widget? for more details on their usage

另外,还有第三个解决方案。这是 GlobalKey

As a side note, there a third solution. Which is GlobalKey

我不会涉及太多细节,因为这更多的是hack,而不是适当的解决方案。 (请参见 Builder vs GlobalKey

I won't go into too many details, as this is more a hack than a proper solution. (see Builder versus GlobalKey)

但是基本上,它允许在 build 调用之外获取任何小部件的状态/上下文。是父母还是孩子

But basically, it allows getting the state/context of any widgets outside of the build call. Be it parents or children.

这篇关于在Flutter中的小部件之间传递数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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