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?