I'm trying to make chat app in Flutter with Firebase and had a trouble which I can't know if my user is active (using the app) or not. I tried to make a native code that run when app terminated but didn't work.
I tried to make it with lifecycle method but didn't work .
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.detached:
FirebaseFirestore.instance.collection("users").doc(id).update({"active" : false});
break;
default:
}
}
But it does nothing when app is terminated. Does anyone have an idea for it in Flutter?
CodePudding user response:
There is no way to guarantee that your update({"active" : false})
code will still be able to execute the write operation when the user is no longer actively using the app. Firestore has no built-in capability to write something in the database after the client has disconnected.
Firebase's other database: the Realtime Database, does have the ability with its onDisconnect
handlers, which are delayed write instructions that you to the database when you are connected, and which the database then executes once it detects that the connection is gone.
You can combine the Realtime Database with Firestore as shown this documentation page on connecting Realtime Database and Firestore to get presence information into Firestore too. I'd also recommend reading the non-Flutter specific documentation on building a presence system with the Realtime Database.