Marko Jerkić

Learner's Deep Learning Blog

Firebase Database – Working With Maps And Lists

I did a blog post on basic Firebase Realtime Database a couple of days ago. In that post, you can find out about the basics of how Firebase stores its data and learn basic queries. You can find that post here. As I said in that article, storing values in a such a way so that you yourself create a new child in the Firebase JSON tree can be useful (for example, when storing some constants), but most of the time, you are going to store some kind of a list, whether that be user’s likes, phone numbers etc. Firebase Realtime Database makes it quite easy to that as well.


Adding lists to the Firebase Realtime Database is really similar to adding a single String. One difference is that you are going to need to use the push() method constantly. Calling the getKey() method on the push() method return a key of the object.

Passing a list is the same as passing a regular String. Create a database reference to your Firebase Database, add children and set the value. But I don’t think that you’re going to pass full lists so often. Most of the time you’re going to push one object at a time.

You could do something like this:

With this layout:

And the User class:

Here, you’re passing a List to the database. That is not very efficient. If you were to use something like Maps, Firebase would provide you with a updateChild() method. This app relies on constantly adding new Users. You could rewrite this bit of code and create a list of lists, so you do not have to delete the existing list then pass a new, updated one to the database.

I think that this is a much better way of writing this app.

This is a shorter way and it is more efficient. Here, we’re passing the User objects one at a time, and then converting them into strings when quiring.

As you can see, Firebase Realtime database provides you with a couple of methods in the ChildEventListener class. The names of the methods explain themselves.

  • onChildAdded() gives a list of item every time a new item is added. It provides you with a DataSnapshot object through which you can retrieve your needed data.
  • onChuldChanegd() listends for any modifications in the child. It also provides you with a DataSnapshot object which contains the updated data.
  • onChildRemoved() listens for when an item has been removed from the child. DataSnapshot contains the data for the removed child.
  • onChildMoved() listens for changes in the order of the item in the child.

Sorting the data

Firebase provides you with a couple of ways of sorting the data. Those are:

  • orderByChild() – orders by the value of the specified child key
  • orderByKey() – order by the key of the child
  • orderByValue() – orders by child values


Using Maps in the Firebase Database is really the same as using the List. The core difference is the difference between Lists and Maps. But, e won’t get into the Computer Science definitions between Maps and Lists in this post.

Filtering data

You can filter the Firebase Database query based on the limit or the range that you set.

  • limitToFirst() – sets the maximum amount of data returned looking from the beginning forward.
  • startAt() – return items greater than or equal to the specified key or value depending on the order-by method chosen.
  • limitToLast() – similar to the limitToFirst() method, it returns the maximum amount of data looking from the end backwards.
  • endAt() – returns items less than or equal to the specified key.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top