写入initState()
函数的代码应该写在super.initState();
之前还是之后?
哪一个是正确的:
@override
// code here
super.initState();
}
或
@override
super.initState();
// code here
}
最佳答案
两者都可以。
但是,如果您从任何依赖项或官方文档中看到 flutter ,请将您的代码写入 initSate()
之后super.initState();
@overrride
initState(){
super.initState()
//your code
}
引用此initState
dispose()
相反,在 super.dispose(); 之前编写代码
@overrride
dispose(){
//your code
super.dispose()
}
引用dispose
当我看到@Kahoo的答案时,我通过cmd +单击super.dispose和super.initstate进行检查,我找到了这个用于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;
}());
}
关于flutter - 代码应该写在 super.initState(); 之前吗?或者在 Flutter 之后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60254613/