whatwhatwhat
whatwhatwhat

Reputation: 2276

In general, should a custom initialize() be executed before or after super.initState()?

Should my custom initialize() run before or after super.initState()? Or does it not matter?

@override
  void initState() {
    super.initState();
    initialize();
  }

OR

@override
  void initState() {
    initialize();
    super.initState();
  }

Upvotes: 0

Views: 28

Answers (1)

Peter Koltai
Peter Koltai

Reputation: 9744

According to the documentation of initState method:

Implementations of this method should start with a call to the inherited method, as in super.initState().

So the answer is that you have to call it first. The reason in my opinion is that the inherited method is responsible for basic initialisation when the widget is inserted into to widget tree, so calling your code before super.initState() could result in undesired behaviour.

Upvotes: 1

Related Questions