繁体   English   中英

检查聊天是否已经存在实时数据库

[英]Check if chat already exists RealTime Database

我正在使用以下数据库结构。

在此处输入图像描述

与官方文档中介绍的相同

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

我想检查是否已经与这些用户聊天,这样他们就不会重复创建。

这就是我正在尝试的

final snapshot = await _database.ref().child("members").child(firstUserId).get();

但这没有找到任何东西,因为chatId是第一个。 有没有办法做到这一点或更好的数据库结构?

我希望你能帮助我。 提前致谢。

我找到了一个解决方案,我首先创建了与成员的聊天。 我应该使用联系人列表创建一个用户,然后像这样检查它:

  Future<bool> createChat(String firstUserId, String secondUserId) async {
    //Check if theres already a chat between the two users
    final snapshot = await _database.ref().child("users/$firstUserId/contacts").get();
    final userContacts = Map<String, dynamic>.from(snapshot.value as Map);
    //Check if the user is already in the contact list
    if (userContacts.containsKey(secondUserId)) {
      return false;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM