Home > Software engineering >  Flutter is there any possible way to make user having online status when the user open the app
Flutter is there any possible way to make user having online status when the user open the app

Time:03-20

how can i create the online status bar when user online and when user offline/closed the app, how can i put something look like "Online 12 minute ago" using firebase. i dont know how it work with flutter and firebase to do that.

CodePudding user response:

You can use

const oneMin = Duration(minutes:1);
Timer.periodic(oneMin, (Timer t){
    //your function to save current time in firebase
    //Can also check last seen of other users here
})

But when you check last seen time of other users, if lastseen is before one minute then user is offline otherwise user is online

CodePudding user response:

You can use WidgetsBindingObserver to manage the user's online/offline status

Add observer


class _HomeState extends State<Home> with WidgetsBindingObserver {
...

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance!.addObserver(this);

  }

Remove observer

  @override
  void dispose() {

    WidgetsBinding.instance!.removeObserver(this);

    super.dispose();
  }

Handle the state

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    if (state == AppLifecycleState.resumed) {

      updateStatus(true);

    } else if (state == AppLifecycleState.paused ||
        state == AppLifecycleState.inactive) {

      updateStatus(false);

    }
  }

how can I put something look like "Online 12 minutes ago" using firebase.

For that, you can store the current timestamp in the firebase when the user is offline. And to show time/date in the ago format you can use timeago

  • Related