How to query firestore data in Flutter where its field value is contained in local list variable

Viewed 5559

I'm working on a Flutter app that shows events details at my university. I'm trying to make a favorite system that lets the user pinned the events by saving its name to a local list variable. So on the favorite page, I'd like to query each entry where its name matches those in the local variable. For example, I have this favorite list

var fav = ['Event 1', 'Event 5']

Here's my code so far

Stream _queryDb() {

Query query = db.collection('Event').orderBy('Start_time');

slides = query.snapshots().map((list) => list.documents.map((doc) => doc.data));
}

This will query every entry in my firestore db

But I want my query to query the entry with the name 'Event 1' and 'Event 5'

How can I do that? Thanks in advance!

PS. This is my first post on StackOverflow so I'm sorry if I'm not clear or I doesn't provide enough explanation/information.

2 Answers

Try this

List<String> events = ["Event 1", "Event 2"];
Query query = Firestore.instance
        .collection('Event')
        .where('name', whereIn: events)
        .orderBy('Start_time');

Just use the where operator on your CollectionReference:

for(var i = 0; i < fav.length; i++){
    Query query = db.collection('Event').orderBy('Start_time').where('name', isEqualTo fav[i]);
    slides = query.snapshots().map((list) => list.documents.map((doc) => doc.data));
}

Assuming each field in the collection has a field called 'name', it will get the specific document.

Related