【问题标题】:How to filter Firestore documents using Flutter?如何使用 Flutter 过滤 Firestore 文档?
【发布时间】:2020-07-13 03:11:58
【问题描述】:

我按照this 教程创建聊天。将消息插入 Firestore 的代码如下

var _firebaseRef = Firestore.instance.collection("chats");
void sendMessage(String message, String idFrom, ProductModel productModel,
  String fromName) {
String groupChatId;
String idTo = productModel.userId;
if (idFrom.hashCode <= productModel.userId.hashCode) {
  groupChatId = '$idFrom-$idTo';
} else {
  groupChatId = '$idTo-$idFrom';
}

var documentReference = _firebaseRef
    .document(groupChatId)
    .collection(groupChatId)
    .document(DateTime.now().millisecondsSinceEpoch.toString());

Firestore.instance.runTransaction((transaction) async {
  await transaction.set(
    documentReference,
    {
      'message': message,
      'idFrom': idFrom,
      "productId": productModel.id,
      "createdAt": utils.currentTime(),
      'timestamp': DateTime.now().millisecondsSinceEpoch.toString(),
      "fromName": fromName,
      "idTo": productModel.userId
    },
  );
});
}

如您所见,DocumentIdCollectionId 上使用的 groupChatId 是 2 个 ID(发送者和接收者)的组合。

这是它的样子:

显示每条消息的代码工作正常

 String groupChatId;
if (idFrom.hashCode <= idTo.hashCode) {
  groupChatId = '$idFrom-$idTo';
} else {
  groupChatId = '$idTo-$idFrom';
}

return _firebaseRef
    .document(groupChatId)
    .collection(groupChatId)
    .orderBy('timestamp', descending: true)
    .snapshots();

在该图片中,您可以看到 1 条聊天和 1 条消息,我正在尝试显示当前用户的每个聊天,为此我会过滤 chat 集合中的所有文档,以便用户可以单击它并在 chatd 详细信息页面上获取列表消息。

我不知道该怎么做,如果您有更好的聊天方法,我将不胜感激

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    您可以调用getDocumentsthen 方法,在其中您可以进行验证,在这种情况下,如果documentID包含您的ID,则可以。像这样

    Firestore.instance.collection("chats").getDocuments().then((value) {
          print(value);
          value.documents.forEach((element) {
            if (element.documentID.contains(currentUserId)) {
              DocumentSnapshot document = element;
              print(document);
            }
          });
        });
    

    应该可以的

    【讨论】:

      【解决方案2】:

      您可以使用类似的方法查询 currentUser Id 等于 IdFrom 或 IdTo 的位置

      StreamBuilder(
                     stream: Firestore.instance
                              .collection("users")
                             
                           .orderBy('createAt', descending: true)
                        .where('idFrom', isEqualTo: currentuserId)
                        .where('idTo', isEqualTo: currentuserId)
                              .snapshots(),....
      

      如果您是第一次运行此程序,请检查您的控制台以获取创建 index 的链接

      【讨论】:

      • 我创建了聊天索引idFrom Ascendente, idTo Ascendente 没有使用Firestore.instance.collection("chats").where("idFrom", isEqualTo: currentUserId).snapshots()
      • 运行你的应用程序时,在控制台中寻找一个链接,打开链接,它会为你创建索引..等待五分钟再试一次
      • 您不能在document 之后使用orderBy。我得到了The method 'orderBy' isn't defined for the type 'DocumentReference'. Try correcting the name to the name of an existing method, or defining a method named 'orderBy'
      • 我已经更新了我的答案..forgot to remove the documnet(); :)不要忘记标记为答案
      猜你喜欢
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2019-05-05
      • 2021-11-29
      • 2020-09-26
      相关资源
      最近更新 更多