Should codes be written before super.initState(); or after in Flutter?
both will work.
But if you see from any dependencies or official docs flutter, write your code in initSate()
after super.initState();
@overrride
initState(){
super.initState()
//your code
}
reference to this initState
the opposite for dispose()
, write your code before super.dispose();
@overrride
dispose(){
//your code
super.dispose()
}
reference to dispose
When I see @Kahoo answer, I check it by cmd + click at super.dispose and super.initstate, I found this for dispose
/// If you override this, make sure to end your method with a call to
/// super.dispose().
///
/// See also:
///
/// * [deactivate], which is called prior to [dispose].
@protected
@mustCallSuper
void dispose() {
assert(_debugLifecycleState == _StateLifecycle.ready);
assert(() {
_debugLifecycleState = _StateLifecycle.defunct;
return true;
}());
}
abstract class State :
/// If you override this, make sure your method starts with a call to
/// super.initState().
@protected
@mustCallSuper
void initState() {
assert(_debugLifecycleState == _StateLifecycle.created);
}