简体   繁体   中英

How to check if my Flutter Web app is currently running?

I am building a chat app in Flutter Web, and would like to add a feature for flagging when the user is "Online".

I want to send a bool to Firebase Firestore whenever the user launches and closes the app (for example, when the user launches the Flutter Web URL = true is being sent to Firestore for isOnline flag. And when the user closes the tap = false is being sent to isOnline ).

For that, I would need a way to detect the lifecycle of the Flutter Web app, and to check whether the app is currently actively running or not.

Does anyone know what is the best way of doing that?

Thank you!

While you can easily update your firestore whenever a user opens your app (just add it to a point executed on launch, like before the runApp() part in your main method). However, there sadly is no guaranteed way to know, if a user closes your app. It could be force closed via task manager etc., robbing you of the ability to detect it. However, firebase offers the following solution to detect presence, see here .

You could also save a timestamp as the isOnline point and let it timeout. You could update it periodically in your app, giving you not a perfect but maybe a good enough idea if a user is online.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM