0
void initUser() async {
    currentUser = await UserController.getUser();

    if (mounted) {
        setState(() => 0);
    }
}

What is this code doing? I've read the documentation but still couldn't understand.

What is this mounted keyword and what does it mean when we do setState(() => 0)? Does doing this set all the states to their initial values?

Hamed
  • 5,867
  • 4
  • 32
  • 56
Milan Poudel
  • 602
  • 1
  • 9
  • 20
  • `mounted` seems to be a variable which has been defined ouside of the scope that you have provided. Where did you get the code from and can you provide more of it? – mamen Jul 12 '21 at 09:31

1 Answers1

2

mounted is the property of State. It sets to true when element of stateful widget adds to elements tree and sets to false when element removes from it. Link to docs. Link to useful article about trees.

What about your code, it is an error to call setState unless mounted is true. So if (mounted) is just for safety.

Mol0ko
  • 2,938
  • 1
  • 18
  • 45