简体   繁体   中英

How to use OR operator in flutter firebase?

My firestore field can contain either the value of gender as 'MEN', 'WOMEN' or 'UNISEX'. So I want to get the documents where the field is either MEN or WOMEN.

QuerySnapshot snap = await myReference.where('gender',isEqualTo:'MEN').limit(6).get();

How can I extend this query for it?

You can use whereIn operator to get documents for required genders.

QuerySnapshot snap = await myReference.where('gender', whereIn:['MEN', 'WOMEN']).limit(6).get();

This query will fetch documents where the field gender is equal to either of the values mentioned in that array passed as value of whereIn

As Dharmaraj mentioned, Cloud Firestore is now supported IN operator. you can use WhereIN operator that support up to 10 comparison values. the code could be:

QuerySnapshot snap = await myReference.where('gender',whereIn:['MEN','WOMEN','UNISEX']).limit(6).get();

Note : this soultion has some other limitations as written in the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM