Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Get reference to first item/element

I'm trying to get a reference to the fist item in a firebase "Array"

In my case it would be "ElmmycgjS1Nvq.../albums/-IrdMMds"

enter image description here

But I'm not sure how to contsturct the query:

dbRef.child("ElmmycgjS.../albums").???.child("name"); // = "Brothers in Arms"

Where ??? should be something like getFirst(). Suppose I don't know the Id of the first Album beforehand (-IrdMMds). I just need to get the first one. I've tried limitToFirst(1).getRef() but this didn't help.

like image 351
frankelot Avatar asked Jul 11 '26 09:07

frankelot


1 Answers

You'll want a query that listens for the first child:

Query first = dbRef.child("ElmmycgjS.../albums").limitToFirst(1);

And then attach a listener to that query.

When you execute a query against the Firebase Database, there will potentially be multiple results. So the result of the query will be a list of those results. Even if there is only a single result, the it will be a list of one result.

like image 133
Frank van Puffelen Avatar answered Jul 14 '26 00:07

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!