Android Arrays and Firebase realtime database

Problem: — Arrays are not stored as array in the firebase realtime database

Handling arrays with firebase realtime database is a bit tricky and they don’t work as expected. Firebase stores them as key value pair, for example

If array contains following
['item1', 'item2', 'item3']
then firebase will store them as{0: 'item1', 1: 'item2', 2: 'item3'}

It works fine if you are saving and retrieving it from the firebase api

List<String> list = new ArrayList();list.add("item1");
list.add("item2");
list.add("item3");
DatabaseReference dbRef=FirebaseDatabase.getInstance().getReference("/path/").child(id);
dbRef.setValue(list);

Problem arises as soon as you delete any item from the firebase console and the key/value deleted comes as null in the Database snapshot.

Solution: — while reading values from the Firebase, make sure you are not getting any null values in the Database snapshot, if null value exists then just remove them, alternate approach is that you modify this database only from firebase api not directly if it contains arrays.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade