2019-05-23

flutter 踩坑日记....

我在使用TabBar配合TabBarView使用的时候遇到了这个问题setState()在dispose()之后调用

FlutterError (setState() called after dispose(): _TimeLineState#fdb61(lifecycle state: defunct, not mounted, ticker inactive) This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback. The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree. This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().)

打印台描述的很清楚 "首选解决方案是取消计时器或停止侦听dispose()回调中的动画。 另一种解决方案是在调用setState()之前检查此对象的“已挂载”属性,以确保该对象仍在树中。"。

我在dispose的时候也把controller停掉了,但是问题并没有解决

在dispose的时候感觉并没有解决到问题,嗯,继续面对google开发,终于在stackoverflow找到同一个问题的帖子,在dispose()之后调用的setState(),于是..........


如图

只需要在setState()前添加一个!mounted的判断当前页面是否存在于构件树中,存在赋值,不存在结束操作。

你可能感兴趣的:(2019-05-23)