Filtering content from Firebase - firebase

So I'm coding a healthy social media app using Flutter and Firebase as my database.
I want to have a filter button where users can filter content based on the 'BoardType' field(in the picture)
Is there any way I can do this?
Firebase screen

of course, you can do so using filters like:
FirebaseFirestore.instance
.collection('posts')
.where("BoardType", isEqualTo: "Book of the Month").get();
which is a future that returns the documents that match your request.

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 add conditionally data to Firebase Firestore?

I'm working on a project which is about to go live, using Google Firebase Firestore for data.
The users will be adding review through a form, but currently that form submit is:
firebase
.firestore()
.collection(props.collection)
.add({
item: entryName,
value: parseInt(rate),
description
})
But honestly I wouldn't really love to let users adding items without my confirmation.
Any idea how it could be done? If not only Firebase Firestore it's fine as well, but I have no ideas.

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.

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