什么时候在设置状态之前检查已安装?
When to check for mounted before setting state?
文档 here 说:
It is an error to call this method after the framework calls dispose.
You can determine whether it is legal to call this method by checking
whether the mounted property is true.
我从未见过检查 mounted
属性 的 Flutter 示例,那么我们应该在哪些情况下进行实际检查呢?
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.
引用自 SDK 源代码
文档 here 说:
It is an error to call this method after the framework calls dispose. You can determine whether it is legal to call this method by checking whether the mounted property is true.
我从未见过检查 mounted
属性 的 Flutter 示例,那么我们应该在哪些情况下进行实际检查呢?
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.
引用自 SDK 源代码