当然我们可以根据错误的提示解决这个问题,在错误的第一行和第三行已经告诉了我们造成错误的原因,原因如下:
lifecycle state: defunct, not mounted Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
通过注释可以知道,除非[mounted]为真,否则调用[setState]是错误的。
所以改进方案
,在使用 setState() 方法之前,先判断一下 mounted
是否为真,代码入下:
if (mounted) {
setState((){
//需要执行的操作
});
}