1.问题描述:
软键盘输入监听动态计算高度中设置setState的调用导致这个问题。
2.问题分析:
State 对象被从对象数卸载释放之后再次调用 setState 就会报 setState() called after dispose()。
3.解决方案:
State 的 mounted 源码:
/// Whether this [State] object is currently in a tree.
///
/// After creating a [State] object and before calling [initState], the
/// framework "mounts" the [State] object by associating it with a
/// [BuildContext]. The [State] object remains mounted until the framework
/// calls [dispose], after which time the framework will never ask the [State]
/// object to [build] again.
///
/// It is an error to call [setState] unless [mounted] is true.
bool get mounted => _element != null;
注释中说得很清楚:判断 State 对象现在还在不在对象树中。
So,解决方案就是在 setState 之前先判断一下该 State 是否已经被释放:
if (mounted) {
setState(() {
//具体的操作
});
}