Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect firebase child value is "null" or "0"

This is the value in inside my firebaseDatabse

This is how I retrieve user infomation (e.g Name & Age)

My questions is how to I check if the child value is "null"? Help and guidance needed...... Thanks

like image 861
Arduino Avatar asked Dec 02 '25 13:12

Arduino


2 Answers

There are a few methods you can call from a DataSnapshot to check if a value is 'null'.

The first thing is to make clear is that Firebase will not store a node or field with a null value, so something like age: null won't be in your database. If the value is null, the age field just won't be there at all.

So the methods available on a DataSnapshot are: exists(), hasChild(String s) and hasChildren() which can be used to check for the existence of something depending on what you're looking for.

Examples of usage would look like:

if (dataSnapshot.exists())

if (dataSnapshot.hasChild("age"))

if (dataSnapshot.child("name").exists())

If you want to check if the value is actually '0' as also mentioned in your question then there is nothing special to that; you would just check if the value equals zero the same as you would in any other situation.

like image 96
Lewis McGeary Avatar answered Dec 05 '25 01:12

Lewis McGeary


your getters will return a null for empty fields in your snapshot

like image 31
T double Avatar answered Dec 05 '25 01:12

T double



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!