在 StatefulWidget 中,我实现了一个deactivate()方法,该方法执行一些清理工作。
实际上,StatefulWidget 会定期轮询服务并使用新接收到的数据重新加载自己。因此,它使用Timer(),它会定期调用服务器轮询回调。
在deactivate()被称为就好了。但是在小部件的某些停用时,异步Timer()仍会触发最后一个事件并调用回调 - 而小部件部分停用。
这是我的停用():
@protected
@mustCallSuper
void deactivate() {
// allow callback of Timeer() to not execute
_timerCanceled = true;
// Cancel the periodic Timer()
_timer.cancel();
// Should be called LAST or FIRST?
super.deactivate();
}
Run Code Online (Sandbox Code Playgroud)
问:一般问题:如何取消一些异步Timer()并确保它不再调用提供的回调。
或者:如何编写取消 a 的Timer()代码并在完成取消操作后执行代码,以便Timer()绝对不会调用的回调。
在 Flutter画廊应用示例中,他们dispose在他们的State类中使用了 override :
@override
void dispose() {
_timeDilationTimer?.cancel();
super.dispose();
}
Run Code Online (Sandbox Code Playgroud)
我不相信在Timer里面创建StatefulWidget. 您可能需要参考图库示例(以及Flutter 存储库中的其他示例)。
| 归档时间: |
|
| 查看次数: |
4464 次 |
| 最近记录: |