Android Arrays and Firebase realtime database
Sep 2, 2018 · 1 min read
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.
