Amine
Amine

Reputation: 1476

Flutter firestore fetch documents with a condition on a map field

So I have a document in this form:

Field:
    NestedField: Value

How can I use the where() method in Flutter to fetch all documents that satisfy a condition on NestedField? I.e:

Firestore.collection("forms").where("Field.NestedField",isEqualsTo: "Op1").getDocuments(). // This returns null

Upvotes: 3

Views: 4954

Answers (1)

Amine
Amine

Reputation: 1476

Alright, so this was caused by another error with no relation to Firestore directly. The Field.NesterForm approach works and it returns the data. Since Firestore uses JSON then it's the correct way to access nested fields this way. The code I was testing with was:

List<DocumentSnapshot> docs;
await Firestore.instance.collection('form')
..where("FirstForm.Operator",isEqualTo: _filter.text)
.getDocuments().then((query) {
    docs = query.documents;
});
print("DOCS: $docs");

The .. before the where() that I didn't see at first caused the awaitto not actually wait for the return so the print of my docs variable always returned null. Once I removed one dot, it works fine now and the equals is working.

Upvotes: 2

Related Questions