Reputation: 8256
what is the difference between running a method before super.initState()
and after super.initState()
@override
void initState() {
super.initState();
getCurrentUser();
}
@override
void initState() {
getCurrentUser();
super.initState();
}
Upvotes: 0
Views: 444
Reputation: 2236
Suggestion Dart/Flutter documentation "suggests" to call super.initState() as first method before your body implementation.
But if we look at how it is implemented Documentation
@protected
@mustCallSuper
void initState() {
assert(_debugLifecycleState == _StateLifecycle.created);
}
it's possible to see that it contains only an assert(). the assert built-in function it's called only in DEBUG mode, not in production mode. So, at the end of the day, it really doesn't matter, because the super.initState() would practically do nothing.
Upvotes: 1
Reputation: 846
Explanation for framework: Dart is class based object oriented programming language, so when you build a widget you extend it from a base class StatelessWidget
or StatefulWidget
. initState
is lifecycle method in flutter widget (as you might know) which is only available to override in StatefulWidgets and it called only once. So, it call initState of base class which is StatefulWidget thats why you call super.initState()
which in turn creates BuildContext and attached State.
Now your question: I didn't find anything different in calling a method before or after super.initState()
. Event I tried adding addPostFrameCallback
before super.initState()
and it worked fine.
Even super.mounted
is also true in both cases.
Upvotes: 2