【问题标题】:Implement realtime online/offline status with flutter and firebase使用 Flutter 和 Firebase 实现实时在线/离线状态
【发布时间】:2020-07-02 05:11:14
【问题描述】:

嘿,如果用户在线或离线,在应用中显示的最佳方式是什么?

前端 -> 颤振

后端 -> Firestore 云和 Firebase 身份验证。

我在 firestore 中有一组包含文档的用户。每个文档都是一个用户并包含“状态”字段。在颤振中,我可以在每次用户登录或注销时更新此字段,但如果您关闭应用程序,它不会更新。

【问题讨论】:

    标签: android ios firebase flutter dart


    【解决方案1】:

    您可以使用WidgetsBindingObserver 扩展您的 statefulWidget State 类 喜欢

    class _HomePageState extends State<HomePage>
        with WidgetsBindingObserver
    

    和initState方法加上WidgetsBinding.instance.addObserver(this);。

    @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
      }
    

    稍后覆盖 didChangeAppLifecycleState 方法

    @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.resumed)
          //TODO: set status to online here in firestore
        else
          //TODO: set status to offline here in firestore
      }
    

    【讨论】:

    • 将此添加到您在 main.dart 文件中使用的 statefulWidget 类中。每当应用程序被恢复并且每次应用程序进入后台时,它都会在您的应用程序中运行。
    • 是否可以检测到用户何时点击查看所有应用图标并滑开应用。
    • 朋友们好,但是 web 怎么样,如何使它适用于 web 版本提前谢谢!
    • 但是,如果我关闭 wifi 然后关闭应用程序,didChangeAppLifecycleState() 将尝试在 Firestore 中将状态设置为“离线”,但由于 wifi 关闭,Firestore 无法将状态设置为离线。 .. 因此,该应用程序的其他用户将看到该用户“在线”。我该如何解决这个问题?
    【解决方案2】:

    这可以通过 WidgetsBindingObserver();

    完成
    class _HomeState extends State<Home> with WidgetsBindingObserver {...}
    

    先初始化

    @override
    void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
    }
    

    添加此函数后监听应用状态didChangeAppLifecycleState()

    String? changes;
    
    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    
    final isBg = state == AppLifecycleState.paused;
    final isClosed = state == AppLifecycleState.detached;
    final isScreen = state == AppLifecycleState.resumed;
    
    isBg || isScreen == true || isClosed == false
        ? setState(() {
          // SET ONLINE
        })
        : setState(() {
          //SET  OFFLINE
    
        });
    print('CHANGES IS : $changes ');
    

    }

    字符串?更改 包含您的应用状态! Be Happy You Can 你想要什么,这可以像 Whatsapp Messenger 一样通知在线状态!

    我的代码:

        isBg || isScreen == true || isClosed == false
        ? setState(() {
          changes = "User is online"
        })
        : setState(() {
          changes = "User is Offline"
        });
        print('CHANGES IS : $changes ');
    

    【讨论】:

      【解决方案3】:

      你可以捕捉到错误。

      您必须将源代码转换为服务器 来源:Source.server

      users.get(GetOptions(source: Source.server))

      final FirebaseFirestore _firestore = FirebaseFirestore.instance;
            final CollectionReference users = _firestore.collection('users');
          
            users.get(GetOptions(source: Source.server))
                .then((QuerySnapshot querySnapshot) {
              querySnapshot.docs.forEach((doc) {
                print(doc["first_name"]);
                print(doc["last_name"]);
                print(doc["gender"]);
                print(doc["phone_number"]);
              });
            }).onError((error, stackTrace) {
              print(error.toString());//Offline
              Global.showSnackBar(context, error.toString(), false);
            });
      

      【讨论】:

        猜你喜欢
        • 2020-11-15
        • 2012-01-17
        • 2016-12-23
        • 1970-01-01
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        • 2020-09-01
        相关资源
        最近更新 更多