Null entries in realtime database?

1,136 views
Skip to first unread message

Alan H

unread,
Aug 31, 2021, 10:14:14 PM8/31/21
to Firebase Google Group

When I view my database in the firebase console, it looks fine -- I see the following

xxxxx-nnnnnn-default-rtdb
- users
   - 1
         email: "m...@somewhere.com"
         role: "admin
         username: "me"
   - 4
         etc.

There is no user 0, 2 or 3, just 1 and 4, so this display is correct and makes sense.

But when I export the users table as json, what I get includes null entries for 0, 2 and 3, in addition to 1 and 4:

   0:   null
   1:
        email: "m...@somewhere.com"
        role: "admin
        username: "me"
   2:   null
   3:   null
   4:
        email: etc.

The data was built with a PHP script using kreait/firebase-php, and it only uploaded the non-null items, corresponding to records in my web server's database.

Also, when I try getting a snapshot of "users" using the Flutter Firebase_database package, I get a null return, even though there are numerous non-null entries I would have expected to be returned in the snapshot.

Any suggestions regarding where the nulls are coming from in the first place, or how to avoid getting a null snapshot in Flutter?









Frank van Puffelen

unread,
Aug 31, 2021, 10:16:31 PM8/31/21
to Firebase Google Group
What you're seeing is the expected behavior, and is due to the fact that Firebase SDKs and the REST API try to interpret sequential, numeric keys as arrays. So your keys 1 and 4 are interpreted as an array of length 5, of which indexes 0, 2 and 3 are empty.

If you don't want the SDK/REST API to do this, always use alphanumeric keys, for example by prefixing the number with a short string: key1, key4.

Also see Kato's classic blog post in best practices for using arrays in Firebase: https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

Alan H

unread,
Sep 2, 2021, 10:49:04 AM9/2/21
to Firebase Google Group
Thank you.  I'll do some further testing with your suggestion to use alphanumeric keys.

Alan H

unread,
Sep 2, 2021, 10:12:22 PM9/2/21
to Firebase Google Group
Thanks again Frank, with your assistance I very easily resolved an issue I had spend 10+ hours on over the last few days.  Prefix the numeric id with "ID" in the PHP script that exports from my web site to Firebase, then strip the "ID" in the flutter app that consumes that data, and everything is working perfectly.

That blog post should be required reading for anyone working with firebase!

On Tuesday, August 31, 2021 at 10:16:31 PM UTC-4 frank.va...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages