flutter中的initState和super.initState是什么? [英] What is initState and super.initState in flutter?

查看:1210
本文介绍了flutter中的initState和super.initState是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中是书面的,但我听不懂.

In the documentation it is written but I am not able to understand.

在将该对象插入树中时调用.

Called when this object is inserted into the tree.

框架将为它创建的每个State对象精确地调用一次此方法.

The framework will call this method exactly once for each State object it creates.

重写此方法以执行初始化,具体取决于将此对象插入到树中的位置(即上下文)或用于配置此对象的小部件(即小部件).

Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).

如果状态的构建方法依赖于本身可以更改状态的对象,例如ChangeNotifier或Stream,或者某个其他可以订阅以接收通知的对象,则该状态应在initState期间订阅该对象,并取消订阅从旧对象中删除,并在didUpdateWidget中更改时订阅新对象,然后在dispose中取消订阅该对象.

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then the State should subscribe to that object during initState, unsubscribe from the old object and subscribe to the new object when it changes in didUpdateWidget, and then unsubscribe from the object in dispose.

您不能通过此方法使用BuildContext.inheritFromWidgetOfExactType.但是,didChangeDependencies将在此方法之后立即调用,并且可以在其中使用BuildContext.inheritFromWidgetOfExactType.

You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.

如果重写此方法,请确保您的方法以对super.initState()的调用开头.

If you override this, make sure your method starts with a call to super.initState().

但是我不确定它的含义.你能解释一下吗?

But I'm not sure about its meaning. Can you explain it?

推荐答案

将@remi记为initState(),是将有状态窗口小部件插入窗口小部件树时调用的方法.

Credit to @Remi, initState() is a method which is called once when the stateful widget is inserted in the widget tree.

如果我们需要进行某种初始化工作(例如注册侦听器),则通常会覆盖此方法,因为与build()不同,此方法仅被调用一次.

We generally override this method if we need to do some sort of initialisation work like registering a listener because unlike build() this method is called once.

要取消注册侦听器(或进行一些后期工作),请覆盖dispose()方法.

And to unregister your listener (or doing some post work), you override dispose()method.

来自此处

State的子类可以重写initState来完成只需要发生一次的工作.例如,重写initState可配置动画或订阅平台服务.需要通过调用super.initState来实现initState的实现

A subclass of State can override initState to do work that needs to happen just once. For example, override initState to configure animations or to subscribe to platform services. Implementations of initState are required to start by calling super.initState

当不再需要状态对象时,框架会在状态对象上调用dispose().覆盖处理功能以执行清理工作.例如,覆盖处置可取消计时器或取消订阅平台服务. Dispose的实现通常以调用super.dispose

When a state object is no longer needed, the framework calls dispose() on the state object. Override the dispose function to do cleanup work. For example, override dispose to cancel timers or to unsubscribe from platform services. Implementations of dispose typically end by calling super.dispose

这篇关于flutter中的initState和super.initState是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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