Q:

How to filter Firestore documents using Flutter?

I follow this tutorial to create a chat. The code to insert message into Firestore is the follow
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
    },
  );
});
}

As you can see the groupChatId to be used on DocumentId and CollectionId is a composition of 2 ids (sender and receiver).
This is what its looks like:

The code to displey each message works fine
 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();

In that picture you can see 1 chat with 1 message, I am trying to display each chat of my current user, to do that I 'd to filter all document inside of chat collection, so the user can click it and get list message on chatd detail page.
I don't know how to do that, if you have a better approach to do the chat I will appreciate it

A:

You can call getDocuments and then methods, inside of it you can make your validations, on this case you can if the documentIDcontains your ID. Like this
Firestore.instance.collection("chats").getDocuments().then((value) {
      print(value);
      value.documents.forEach((element) {
        if (element.documentID.contains(currentUserId)) {
          DocumentSnapshot document = element;
          print(document);
        }
      });
    });

It should works

