I'm making a voice chat app using Flutter for iOS and Android. I use Firebase Firestore to keep active users in a chat room. When a user kills an app (by swiping up in app list), I have no way to detect it. So there is no chance to remove a user from Firestore users field. I'd like to keep a user in firestore while an app is background. How to successfully know when a user leaves an app? Btw, I'm using Agora RTC SDK for voice call. Thank you for reading.
Firestore has the below structure
chatRoom (collection)
- docidA (document)
- users: ["name1", "name2"] (Array field)
Source code:
class RoomPage extends StatefulWidget {
const RoomPage({Key key,}) : super(key: key);
@override
_RoomPageState createState() => _RoomPageState();
}
class _RoomPageState extends State<RoomPage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// Join into a chat and add myself to firebase firestore users field
addMyselfToFirestore("docidA", "name1");
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() { // Unfortunately Not called when a user kills this app.
super.dispose();
removeMyselfFromFirestore("docidA", "name1");
WidgetsBinding.instance.removeObserver(this);
}
@override
build(BuildContext context) {...}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
// Unfortunately detached not called when a user kills the app
case AppLifecycleState.detached:
removeMyselfFromFirestore("docidA", "name1");
break;
}
}
}
Flutter
$ flutter --version
Flutter 2.0.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b1395592de (7 days ago) • 2021-04-01 14:25:01 -0700
Engine • revision 2dce47073a
Tools • Dart 2.12.2