Firebase @PropertyName doesn't work
Finally got a chance to solve this problem. Thanks to @hatboysam for the suggestion.
The only problem was, @PropertyName
annotation was not properly documented in Firebase.
The first thing that is necessary is the the field must be public otherwise the annotation will not work, which is quite obvious/
Now the annotation takes into account both the field name as well as the getter/setter names to serialize. I also had the problem where the fields as well as the getter/setters were getting serialized, resulting in duplicate key/value pairs.
I solved the problem by using the annotation on the field name which were public and ignoring the getter/setters. This solved the problem perfectly. Not the data was properly serialized with the property name I wanted and there was no duplicate data problem as well.
Here is a simple example,
class Item {
@PropertyName("item_no")
int mItemNo;
// Simplified for brevity
@Exclude
public int getItemNo(){
return mItemNo;
}
@Exclude
public void setItemNo(int itemNo){
this.mItemNo = itemNo;
}
}
Firestore @PropertyName annotation giving strange error?
You are getting the following error:
Found setter on com...***.models.MHatchery with invalid case-sensitive name: setHName
Because the names of the fields in your MHatchery
class are different from the ones in the database. For example, in your class, you have a property called hName
, with a capital N
, while in your database the field is called hname
, with a lower-case n
. To be able to map a Firestore document into an object of type MHatchery
, the name of the properties must match.
There are two ways in which you can solve this issue. The first one would be to change the name of the fields in your class to match the ones in the database. So you should change:
var hName:String,
To:
var hname:String,
And remove the annotation:
//@set:PropertyName("hName")
Or you add the following annotations in front of the property like this:
@get:PropertyName("hName")
@set:PropertyName("hName")
var hName:String,
Getting null while getting data from Firebase RTD
The property in the JSON of the database is called user_uid
, but in your Java code you have:
public String getUserUID() {
return user_uid;
}
Firebase follows JavaBean property naming conventions, so the getter for property user_uid
should be:
public String getUser_uid() {
return user_uid;
}
Similarly the setter should be setUser_uid(String newUid)
, or the field should be user_uid
if that is public.
Alternatively you can change the property name in the JSON to userUID
, so that it matches your code, or use annotations to explicitly tell Firebase the name of the property in the database:
@PropertyName("user_uid")
public String getUserUID() {
return user_uid;
}
Also check out:
- Firebase @PropertyName doesn't work
- Firebase serialization names
- Make first letter of child name capital in Firebase
- Firebase @PropertyName doesn't work, in case you (or someone else who finds this) is having the same problem in Kotlin.
Data output from the FireBase database
Your JSON contains this property for a user:
"EMAIL" : "evgeniy1900@gmail.com",
Which you likely want to map to this in your code:
var Email: String = ""
But Firebase uses JavaBean naming conventions when mapping, which means that your JSON actually maps to a property in Kotlin as:
var eMAIL: String = ""
If you want to maintain both the name on JSON and in Kotlin, you can use a PropertyName
annotation:
@PropertyName("EMAIL")
var Email: String = ""
Also see: Firebase @PropertyName doesn't work and probably others from this search.
Related Topics
How to Click or Tap on a Textview Text
Fragment Oncreateview and Onactivitycreated Called Twice
Using Service to Run Background and Create Notification
Update Listview Dynamically with Adapter
How to Implement Drag and Drop in Android 2.2
Standard Android Menu Icons, for Example Refresh
Recyclerview Changing Items During Scroll
Expandablelistview - Hide Indicator for Groups with No Children
A Failure Occurred While Executing Com.Android.Build.Gradle.Internal.Tasks
Android Autocompletetextview with Custom Adapter Filtering Not Working
Locationlistener of Network_Provider Is Enabled But , Onlocationchanged Is Never Called
Error Message:This Android Sdk Requires Android Developer Toolkit Version 22.6.1 or Above
Checkselfpermission Method Is Not Working in Targetsdkversion 22
My Current Location Always Returns Null. How to Fix This
Android - Key Dispatching Timed Out
Sms Authentication on Android Nullpointerexception
How Does the Getview() Method Work When Creating Your Own Custom Adapter