使用Flutter Android和iOS模块,您可以使用AppLifeCycleState来检测何时将应用程序置于后台。
Flutter Web是否有类似的东西可以检测到浏览器/选项卡关闭或刷新?
如果用户关闭浏览器或刷新,我本质上是想运行Firestore命令。
答案 0 :(得分:2)
您可以使用功能“ onBeforeUnload”来检查选项卡是否已关闭。它可能还会检测页面刷新。
import 'dart:html' as html;
html.window.onBeforeUnload.listen((event) async{
// do something
});
或
import 'dart:html' as html;
html.window.onUnload.listen((event) async{
// do something
});
答案 1 :(得分:1)
只需使用 OnBeforeUnload 或 OnUnload。 对我来说它在没有异步的情况下工作,我认为这是因为窗口不等待关闭它只是关闭。 如果您想在用户退出选项卡或刷新时更改数据库中的某些内容,您应该在后台线程上调用一个方法,以便它在窗口关闭/选项卡关闭/刷新后继续运行并完成任务。
html.window.onBeforeUnload.listen((event) {
// change something in db
});