在initState中调用SetState的重要性 [英] Importance of Calling SetState inside initState

查看:57
本文介绍了在initState中调用SetState的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否应该在StatefullWidgetinitState()方法内部调用setState()方法?

Should setState() method be called inside initState() method of a StatefullWidget?

我的理解是initState()方法将自动应用状态.

My understanding is that initState() method will automatically apply the state.

下面的代码不起作用. post对象的值为空.

The code below does not work. The post object is evaluated as null.

  @override
  void initState() {
    ItemService.getItemById(widget.postId).then((DocumentSnapshot doc){
        post = ItemService.getPostFromDocument(doc);
    });
  }

但是以下方法可行.

  @override
  void initState() {
    ItemService.getItemById(widget.postId).then((DocumentSnapshot doc){
      setState((){
        post = ItemService.getPostFromDocument(doc);
      });
    });
  }

在某些情况下,即使在同一个类中不使用setState(),也都可以正常工作.

Some other cases, all works fine even without using setState() in the same class.

那么我什么时候应该在initState()方法中使用setState(),什么时候不使用?

So when should I use setState() inside initState() method and when not to?

另一个相关问题:

我什么时候应该在我的initState()内部呼叫super.initState()?我不打扰没关系吗?

When should I call super.initState() inside my initState()? Does it matter if I didn't call?

推荐答案

setState()方法通知框架有状态窗口小部件的内部状态已更改.调用此方法将触发窗口小部件以最新状态值进行重建,因此不必在initState()生命周期方法中调用它,因为仅在将窗口小部件插入窗口小部件树中时(即,当小部件已初始化).

The setState() method notifies the framework that the internal state of the Stateful widget has changed. Calling this method is what triggers the widget to rebuild with the latest state values, so it is not necessary to call it inside the initState() lifecycle method since it is only called once when the widget is inserted into the widget tree (i.e. when the widget is initialized).

您可以在此处详细了解setState()方法: setState方法

You can read more about the setState() method here: setState method

关于initState()生命周期方法,每当您覆盖此方法时,您必须在方法的开始或结束处调用super.initState();,否则,小部件将遇到一些问题.诸如未将小部件插入小部件树的问题.

As for the initState() lifecycle method, whenever you override this method you MUST call super.initState(); at the start or end of your method, otherwise, you'll encounter some problems with your widget. Problems like the widget not being inserted into the widget tree.

唯一可以在initState()中使用setState()的地方是在回调函数中,就像在第二个代码片段中所做的那样.之所以起作用,是因为在运行回调时,该窗口小部件已经被初始化并插入到窗口小部件树中,并且内部状态需要更新以触发重建.

The only time you can use setState() inside initState() is within a callback function as you did in the second code snippet. It works because, by the time the callback is run, the widget has already been initialized and inserted into the widget tree and the internal state needs to be updated to trigger a rebuild.

此外,请注意,setState()仅在安装了小部件时才起作用.因此,每个小部件都有一个bool this.mounted属性,如果不确定在调用setState()时是否仍会挂载该小部件,可以检查该属性.未挂载窗口小部件时调用它可能会使您的应用程序崩溃.因此,建议不要在小部件类外部调用setState().

Also, just take note that setState() will only work if the widget is mounted. For this reason, every widget has a bool this.mounted property which you can check in case you're not certain if the widget will still be mounted when setState() is called. Calling it when the widget is not mounted might crash your app. So I'd advice against calling setState() outside the widget class.

这篇关于在initState中调用SetState的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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