简体   繁体   中英

How I can get current active users from Firebase with Flutter?

I am making an application using flutter and firebase. I need to get active users from firebase. I am trying by creating a boolean field in firebase collection with name isActive = false and initiate a function with name _checkInternetConnectivity(); in this function I set when the internet connection is available update value of isActive = true in firebase and when there is no internet connection update value isActive = false so far my code is working perfectly but problem is that when internet connection is available it set the value of isActive = true but when there is no internet connection it unable to update the value isActive = false .
code is here:

void initState() {
    super.initState();
    _checkInternetConnectivity();
}

_checkInternetConnectivity() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      FirebaseFirestore.instance.collection('Profile').doc(user.uid).update({
        'isActive': true,
      });
      print('done mobile');
    } else if (connectivityResult == ConnectivityResult.wifi) {
      _addData.doc(user.uid).update({
        'isActive': true,
      });
      print('done wifi');
    } else if (connectivityResult == ConnectivityResult.none) {
      FirebaseFirestore.instance.collection('Profile').doc(user.uid).update({
        'isActive': false,
      });
      print('done none');
    }
  }
}

so, how I can improve this code or any other way by which I can get active users from firebase with flutter.

Regards

OnDisconnect

The OnDisconnect class is used to manage operations that will be run on the server when this client disconnects. It can be used to add or remove data based on a client's connection status. It is very useful in applications looking for 'presence' functionality.

Instances of this class are obtained by calling onDisconnect on a Firebase Database ref.

Your solution is here

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