'Streambuilder from firebase firestore with advanced filtering
Based on new version of firebase dependencies in flutter, there is new filters such like isNotEqualTo, arrayContains, arrayContainsAny, whereIn and so on.
I try to create streambuilder with some filters, I want to get documents that matches these conditions.
- The "uid" field value Does not equal current uid. (isNotEqualTo)
- The "school" field value isEqualTo current user school.(isEqualTo)
- The "random" field value isGreaterThanOrEqualTo random number that the app generated it (isGreaterThanOrEqualTo)
stream: fireStore.collection(USERS_COLLECTION).where(UID_FIELD, isNotEqualTo: me.uid).where(SCHOOL_FIELD, isEqualTo: me.school).where(RANDOM_FIELD, isGreaterThanOrEqualTo: random).limit(10).snapshots(),
Condition 1 (isNotEqualTo) does not work with me, but 2 and 3 it's working fine, when I add the filter 1 there is error showing to me.
All where filters with an inequality (<, <=, >, or >=) must be on the same field. But you have inequality filters on 'FieldPath([uid])' and 'FieldPath([random])'. 'package:cloud_firestore/src/query.dart': Failed assertion: line 486 pos 18: 'hasInequality == field'
Solution 1:[1]
That is a Firestore-Limitation https://firebase.google.com/docs/firestore/query-data/queries
Note the following limitations for != queries:
Only documents where the given field exists can match the query. You can't combine not-in and != in a compound query. In a compound query, range (<, <=, >, >=) and != comparisons must all filter on the same field.
So you can either filter using != on the uid OR you can filter using >= on Random. You cannot do both in the same query.
I'd recommend filtering without the UID and let your repository filter out the UID later, once you've received the snapshots.
Solution 2:[2]
actually you can filter on multiple fields by creating a composite index.
Cloud Firestore uses composite indexes to support queries not already supported by single-field indexes.
Cloud Firestore does not automatically create composite indexes like it does for single-field indexes because of the large number of possible field combinations. Instead, Cloud Firestore helps you identify and create required composite indexes as you build your app.
If you attempt the query above without first creating the required index, Cloud Firestore returns an error message containing a link you can follow to create the missing index. This happens any time you attempt a query not supported by an index. You can also define and manage composite indexes manually by using the console or by using the Firebase CLI. For more on creating and managing composite indexes, see Managing Indexes.
https://firebase.google.com/docs/firestore/query-data/index-overview#composite_indexes
Also if in your code you call requireData
from your snapshot you will see in your logs a direct link to create that index on Firebase.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dkap |
Solution 2 | Floran |