Flutter: How to count children in Firebase? - firebase

We have a data model designed in the Firebase for realtime questions and answers. However, we are unable to get realtime counts for a quiz.
On researching we are able to see a method addValueEventListener() which refers the DataSnaphot object with the getChildrenCount()
One of the reference links for the above info is: android firebase - get childrens count
However, we are unable to find these methods in the Flutter package for Firebase. The link to the package that we are using: https://pub.dartlang.org/packages/firebase_database
Please advise.Thanks.

It indeed looks like the child count method is not implement in the FlutterFire library version of DataSnapshot. I'd recommend filing a feature request for it here.
The fastest way to get the feature added would probably to create a PR for it yourself, but that would require you to dig into how the FlutterFire library works under the hood.
Alternatively you can listen to the onChildAdded event, and count the children in your client-side Flutter code. It won't be significantly less efficient, since the regular SDKs also need to download all child nodes to be able to return their count.

Related

How to use Firebase built-in features instead of ORM?

I started using Firebase for Android app with Kotlin. So the question is how to use Firebase built-in features instead of adding ORM? I would like to work with classes and objects, instead of dealing with get, set, onSuccessListener and other firebase methods.. So I can create some semi-ORM by myself, by dividing everything to adapter and models. However is there simpler solution? I assume there should be some built in features in Firebase?
E. g. we can query documents with Firebase functions and map them to classes as per official manual:
val docRef = db.collection("cities").document("BJ")
docRef.get().addOnSuccessListener { documentSnapshot ->
val city = documentSnapshot.toObject<City>()
}
To make controller loose coupled from the database, we can create classes AbstractModel and CityModel and Queries and FirebaseAdapter so finally we can do something like
val city = Cities("BJ")
instead of lengthy code above.
Eventually I see that problem is we need to create our own ORM for using Firebase for every project. Or is there simpler solution? Do some existing ORMs work with Firebase? I did some research, however Stackoverflow policy prohibits to post trademarks into the question.

Optimising network connections of firebase cloud function

Firebase documentation recommends including code snippet given at (https://firebase.google.com/docs/functions/networking#https_requests) to optimize the networking, but few details are missing. Like
How exactly does this help?
Are we supposed to call the function defined as per of recommendation
or include this snippet deploy?
Any documentation around this would be of great help.
this is an example showing you how you would make this request, the key part in this example is the agent field which by nature isn't normally managed within your app. By injecting a reference to it manually, you are able to micro-manage it and it's events directly.
As for the second question, it really depends on your cloud function needs - some users set it in a global object that they manage with all cloud functions but it's on a by-use case basis. but ultimately isn't required.
You can read more about HTTP.Agent's and their usage below:
https://nodejs.org/api/http.html
https://www.tabnine.com/code/javascript/functions/http/Agent
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

Should my Flutter App instantiate a new object if I'm using Firestore as a backend?

for the technically-savy people out there. I'm building a flutter app with a firestore backend and I've been doing some research as to the best way to structure my models. Tutorials online show different methods and I can't figure out what's the best one as I want my app to be light but also use Firestore efficiently so it doesn't cost too much.
Those are the ways I've encountered so far:
Have model for the flutter object and another one for the firestore object. Everytime I get the data from firestore I instanciate a firestore object and map it into a flutter object or create a new flutter object and then have a listener there to update my whole app.
Have one model for the flutter/firestore object. Everytime I get data from firestore I need to instantiate it once. No mapping. I have a listener there.
Get the data directly from firestore without needing to instantiate an object and print the documents there, and have a streamprovider to get the data.
I'd really appreciate your help in structuring my app/project. Thanks.
I think you should consider all your requirements, technical and business. The definition of your architecture should entails this needs. If you rush to choose an architecture that in the future would be needed to be re-defined, then it could be more expensive.
Once you have considered this, then you should think in your architecture. Try to share your general workflow.
I want my app to be light but also use Firestore efficiently so it doesn't cost too much.
All the three options you have shared fit to your needs? Is there a step in your solution in which the steps of your three options would fail?
I think you should base the architecture more on your needs than in which is least cost.

Firebase - Admin.firestore vs functions.firestore

I'm newbie on cloud function. I have some confusions.
Is there any difference between admin.firestore and
functions.firestore?
Is admin.database for real-time database?
So if cloud functions are basically written as JavaScript or
TypeScript in Node.js environment, then I can write them with
JavaScript. But documentation made me confused about that. Because
whenever I read the documentation, it gives things different. For
example,
Above code to get a document, it uses document('some/doc').
But, above code it uses doc('doc') to achieve the same functionality. If all of them come form firestore, why do both differ each other?
Can someone help me to understand these questions? Thank you for all your supports.
functions.firestore: is used to set up a trigger. You can set up a listener on a document path so that whenever an event happens on that document (create/update/delete etc.), this function will be executed. See how to create one here.
admin.firestore: is used to create a reference to a firestore database. You can perform various operations on firestore collection/documents using admin sdk through that reference. See documentation.

Does Datastore support bulk updates?

I've combed through GC DataStore documentation, but I haven't found anything useful around bulk updating documents. Does anyone have best practice around bulk updates in DataStore? Is that even a valid use case for DataStore? (I'm thinking something along the lines of MongoDB Bulk.find.update or db.Collection.bulkWrite)
You can use the projects.commit API call to write multiple changes at once. Most client libraries have a way to do this, for example in python use put_multi.
In C# the DatastoreDb.Insert method has an overload for adding multiple entities at once.
If this is a one-off, consider using gcloud's import function.

Resources