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

查看:18
本文介绍了在 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.

下面的代码不起作用.帖子对象被评估为空.

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() 方法通知框架 Stateful 小部件的内部状态已更改.调用这个方法是触发widget用最新状态值重建的原因,所以没有必要在initState()生命周期方法中调用它,因为它只在widget插入时调用一次小部件树(即小部件初始化时).

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天全站免登陆