maxpill
maxpill

Reputation: 1331

Firebase, Flutter - read, retrieve filtered data

My Firebase Firestore data structure looks like this

/users/user1/data/userinfo (String name: Jack, String surname: Johnson)
/users/user2/data/userinfo (String name: Tom, String surname: Ronson)
/users/user3/data/userinfo (String name: Jack, String surname: Dalton)
/users/user4/data/userinfo (String name: Bob, String surname: Smith)

In brackets is the content of the respective userinfo documents

And I want to retrieve all the userinfo documents from the /users/ collection where name == Jack, so I should retrieve /users/user1/data/userinfo(name: Jack, surname: Johnson) and /users/user3/data/userinfo(name: Jack, surname: Dalton). Could someone help me how to do it in Flutter without retrieving the whole /users/ collection and filtering the data on the device, locally.

Thank you :)

Upvotes: 0

Views: 346

Answers (1)

Miguel Ruivo
Miguel Ruivo

Reputation: 17736

This requires only a simple query to your collection.

final documents = await usersCollection.where("name", isEqualTo: "Jack").getDocuments();

documents.forEach((doc) => print(doc.data));

Upvotes: 2

Related Questions