Hamza
Hamza

Reputation: 2045

Firebase firestore @PropertyName not working

I am trying to get data from firestore my Pojo class filed has a different name than firestore, this is why I used @PropertyName("Email") but firestore mapper not mapping the fileds, where i making the mistake?

GmailCredentials.java

public class GmailCredentials {

   private String email;
   private String password;


   public GmailCredentials() {
   }

   @PropertyName("Email")
   public String getEmail() {
      return email;
   }
   @PropertyName("Password")
   public String getPassword() {
      return password;
   }
   @PropertyName("Email")
   public void setEmail(String email) {
      this.email = email;
   }
   @PropertyName("Password")
   public void setPassword(String password) {
      this.password = password;
   }
}

This is how i am reading data from firebase in kotlin

val taskFirebae = Firebase.firestore.collection("AppSettings").document("jt86iH0kJyNhOHftv7DF").get()

            val result = Tasks.await(taskFirebae)
           val gmailCredentials=  result.toObject(GmailCredentials::class.java)


if (BuildConfig.DEBUG){
                Log.d(TAG," ${result.data?.toString()}")
                Log.d(TAG,"emil ${gmailCredentials?.email}")
                Log.d(TAG,"password ${gmailCredentials?.password}")
            }

enter image description here

Screen shot of logs, you can see data is comping from firebase

Upvotes: 0

Views: 161

Answers (1)

Hamza
Hamza

Reputation: 2045

My bad i was making a mistake GmailCredentials is map, so first you have to get GmailCredentials then you get email and password, you can use Map or create new java and encapsulate the GamilCredentials inside that class e.g.

public
class AppSettings {

    @PropertyName("GmailCredentials")
    public GmailCredentials gmailCredentials;

    public AppSettings() {
    }
}

Then you can get like this

val result = Tasks.await(taskFirebae)
           val gmailCredentials=  result.toObject(AppSettings::class.java)

if (BuildConfig.DEBUG){
                Log.d(TAG, ""+{result.data})
                Log.d(TAG,"emil ${gmailCredentials?.gmailCredentials?.email}")
                Log.d(TAG,"password ${gmailCredentials?.gmailCredentials?.password}")
            }

Upvotes: 1

Related Questions