Firebase Query in Flutter Not Returning Results - firebase

I have 2 queries in my flutter app that reference the same information in firebase. One is filtered by the user's id and the other is filtered by the patient's id. Both queries work but only the query that is filtered by uid works with an orderby. Why isn't the second query working?
Query filtered by user id
FirebaseFirestore.instance
.collection('alerts')
.where('access', arrayContains: FirebaseAuth.instance.currentUser.uid)
.orderBy('createdAt', descending: true)
.snapshots(),
Query filtered by patient ID
FirebaseFirestore.instance
.collection('alerts')
.where('patientId', isEqualTo: patient.id)
//.orderBy('createdAt', descending: false)
.snapshots(),
I've commented out the orderBy in this query because it causes it to not return any results. Without using the orderBy I get the results I want just not in the order I want them.
The Alerts have a field called 'access' that contains user ids if that alert should be associated with that user. The 'patient id' on Alerts relates the alert to a specific patient.
The first query returns all alerts that should be viewed by the user.
The second query returns just the alerts for a specific patient.

You need to create indexes for both queries in your Firebase.

Coming back to this because I actually ended up fixing it. Like the other user mentioned I needed to create indexes in firebase in order for this to work. So I went to firebase created an index for my alerts collection and added patientID and creationDate both as descending to an index. Saved that index, waited for it to post to firebase, went back to flutter and it worked.
After that I ran into another issue where the alerts were not displaying in the right order so I simply changed the descending in the orderBy to true instead of false and it all worked out.

Related

fetch limited number of document from Firestore in Flutter

I'm trying to fetch a certain number of document for viewing it in a limit view table, for example 50 elements at the time, whenever the user clicks next it loads the next 50 element form the Firestore and shows it in the table, in order to reduce the data loaded from the firebase and not to slow down the application, since there may be millions of documents stored in the Firestore,
I've tried this solution but is it the best practice?
Thanks.
vehiclesCollection.snapshots().skip(10).take(10).map(
(event) {
print('some logic');
},
);
According to the Firebase docs and FlutterFire docs batching is possible!
It looks like this:
FirebaseFirestore.instance
.collection('users')
.limit(50)
.get()
.then(...);
In order to make it a working pagination you could add startAfterDocument to your query and give as parameter for each request the last element from the previously returned collection:
FirebaseFirestore.instance
.collection('users')
.limit(50)
.startAfterDocument(lastDocument)
.get()
.then(...);

How to make a query from a nested collection in firestore using flutter

I have a nested collection in firestore that I want to make a query from it.
As you can see the first collection called 'businessUsers' and the nested one called 'campaigns',
If I make a query for a field in the 'businessUsers' it's working OK:
return FirebaseFirestore.instance.collection("businessUsers").where('xxx',
isEqualTo:filterBusinessResult ).snapshots().map(_businessListFromSnapshot);
but how can I make a query to 'campaigns' collection field?
I tried
return FirebaseFirestore.instance.collection("businessUsers").doc().collection("campaigns").where('campaignCategory', isEqualTo:filterBusinessResult ).snapshots()
.map(_businessListFromSnapshot);
but it wont work.
Its important to note, that I need all the data with 'campaignCategory' == filterBusinessResult
any idea?
It depends on whether you want to query a specific user's campaigns, or the campaigns of all users together.
If you want to query the campaigns of a specific user, you need to query under their document:
FirebaseFirestore.instance
.collection("businessUsers").doc("your user ID").
.collection("campaigns")
.where('campaignCategory', isEqualTo:filterBusinessResult)
So you have to know the ID of the businessUsers document here.
If you want to query across all campaigns subcollections are once, that is known as a collection group query and would look like this:
FirebaseFirestore.instance
.collectionGroup("campaigns")
.where('campaignCategory', isEqualTo:filterBusinessResult)
The results are going to documents from the campaigns collection only, but you can look up the parent document reference for each DocumentSnapshot with docSnapshot.reference.parent.parent.
When querying something, .doc() requires the id of the document you're trying to get. So it's failing because it doesn't know which document in the businessUsers collection you're trying to fetch a subcollection on. You probably want to use a .collectionGroup() query here. They let you query all subcollections at once (see documentation here). With FlutterFire specifically, it's going to be something like:
var snapshots = FirebaseFirestore.instance.collectionGroup("campaigns")
.where("campaignCategory", isEqualTo: filterBusinessResult)
.snapshots();

How to fetch only documentIDs [Firebase-Flutter]

In my firebase's database I have Users and every user has multiple stores that he ordered from and every store has multiple orders.
To fetch all the orders for specific store I write the following query and it works fine.
QuerySnapshot result = await Firestore.instance
.collection('users')
.document(userID)
.collection('Stores').
document(storeID).getDocuments();
However, I do not need that. I just need the stores that the user ordered from. In other words, I need list of storeIDs for specific user.
Here is my code but it doesn't work.
QuerySnapshot result = await Firestore.instance
.collection('users')
.document(userID)
.collection('Stores').getDocuments();
I just want the IDs
It's not possible with web and mobile clients to get only the IDs without the entire set of documents and their data. If you want a list of all IDs for all documents in a collection or subcolleciton, you're going to have to query that collection as you currently show in the second example. You can easily extract the IDs from the returned documents in the QuerySnapshot. But you are still paying the cost of a document read for each document in the collection, and waiting for all of their contents to transfer.
If you want, you can create your own backend endpoint API to use a server SDK to query for only the IDs, and return only the IDs, but you are still paying a document read for document.

Query Firebase Firestore Subcollection in Flutter

I am querying my Firestore database as follows:
final Query roasters = Firestore.instance
.collection('retailers')
.where('retail_category', isEqualTo: 'Coffee Roasters');
I am receiving the result back of all documents in the retailers collection which have 'retail_category' set as 'Coffee Roasters'.
The Problem
I instead want to turn retail_category into a separate collection and instead reference it in the retailer field (which would negate the following reference):
"retail_categories/hEN5fzNl2hEc2tEs05Wi"
I have tried the following:
final Query roasters = Firestore.instance
.collection('retailers')
.where('retail_categories', isEqualTo: ' qretail_categories/hEN5fzNl2hEc2tEs05Wi');
Here is my Firestore configuration:
Retailers
Retail Categories
It's not possible to reference data in documents outside of the collection being used for the query. For this reason, it's very common to duplicate data between documents that need to be used in queries for each collection. If you don't want to do that, you will have to make separate queries for each referenced document.

Filtering & Sorting Firebase (Firestore) Data on a Flutter App

If my user database is on Firestore and on Registration I ask each users a set of few questions (checkboxes, radio buttons, etc). I want to create a Filter and Sort mechanism where a user is able to filter and sort a list of other users (from Firestore) based on multiple filter parameters sorted according to a particular field.
Is what I want to create even possible on Firebase (FireStore) and Flutter or do I need anything else?
Can anybody please link me to any relating Flutter/Firebase documentation, YouTube videos, or any other relevant content.
Once you fetch your list of users from Firestore, you can easily sort and filter them using the methods provided by the List class in Flutter:
sort method for sorting: https://docs.flutter.io/flutter/dart-core/List/sort.html
retainWhere method for filtering: https://docs.flutter.io/flutter/dart-core/List/retainWhere.html
You can create multiple filter and ordering in firestore in flutter app.
QuerySnapshot orderQuerySnapshot = await orderCollection
.where("status", isEqualTo: status)
.where("timestamp", isGreaterThanOrEqualTo: lowerDate)
.where("timestamp", isLessThanOrEqualTo: upperDate)
.orderBy('timestamp', descending: true)
.get();

Resources