Flutter Web:检测浏览器/选项卡关闭还是刷新? [英] Flutter Web: Detect browser/tab close or refresh?

查看:371
本文介绍了Flutter Web:检测浏览器/选项卡关闭还是刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助Flutter Android和iOS模块,您可以使用AppLifeCycleState来检测何时将应用程序置于后台.

With Flutter Android and iOS modules you can use AppLifeCycleState to detect when the app is put into the background.

Flutter Web是否有类似的东西,可以检测到浏览器/选项卡的关闭或刷新?

Is there something similar for Flutter Web where I can detect the browser/tab closing or refreshing?

如果用户关闭浏览器或刷新,我本质上是想运行Firestore命令.

I essentially want to run a Firestore command if the person closes their browser or refreshes.

推荐答案

您可以使用函数"onBeforeUnload"检查选项卡是否已关闭.它可能还会检测页面刷新.

You can use the function "onBeforeUnload" to check if the tab is being closed. It might detect page refresh also.

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
});

这篇关于Flutter Web:检测浏览器/选项卡关闭还是刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆