43

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

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);
    });
  }

But the below works.

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

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

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

Another Relevant Questions:

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

4
  • 1
    I'm not sure, but maybe your first example doesn't work because you don't call super.initState(). Did you try it? Commented Nov 18, 2018 at 17:46
  • Haven't tried.. But how super.initState() has an impact? I am trying to understand how it works. I doubt it because in the same class, other states work fine even without calling supers initState.
    – Purus
    Commented Nov 18, 2018 at 17:49
  • stackoverflow.com/q/52295949/10269042 Commented Nov 19, 2018 at 8:46
  • Already seen. It explains what is initState and does not explain my question.
    – Purus
    Commented Nov 19, 2018 at 9:40

3 Answers 3

64

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).

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

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.

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.

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.

0
30

You don't need to use setState within initState. In fact, it will not work if you do so.

The thing is, you are not calling setState within initState in your example.

What you do is calling setState on an asynchronous event. But since it's asynchronous, the initState method has already finished

1
  • 6
    Thanks. But its confusing.. Should I be wrapping all my async calls within the setState method inside initState? or should not use it at all? Your answer provides 2 different meaning. Can you please explain or post a simple example..
    – Purus
    Commented Nov 19, 2018 at 4:32
0

You have to use setstate() if you want to call an async method inside the initstate() but if it's not an async method then you'll face a trouble because you'd be trying to rebuild state of the widget even before it is built. Basically the answer is yes: you should call setstate() all the time you are to execute an asynchronous function inside initstate(). I strongly reccomend you search about lifecycle of widgets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.