Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase "User is missing a constructor with no arguments"

Tags:

I am trying to retrieve my user details from the /User/ tree i have in Firebase. I have the following User object :

public class User {     private String name;     private String email;     private FirebaseUser firebaseUser;     private String lastOnline;     private LatLng latLng;     private ArrayList<SwapLocation> locations;      public User(){ }      public String getName(){         if(firebaseUser.getDisplayName() != null && !firebaseUser.getDisplayName().equals(""))             return firebaseUser.getDisplayName();          if(name != null && !name.equals(""))             return name;          return "";     }     public void setName(String name){         this.name = name;     }      public String getEmail(){         if(firebaseUser.getEmail() != null && firebaseUser.getEmail().equals(""))             return firebaseUser.getEmail();          if(email != null && email.equals(""))             return email;          return "";     }     public void setEmail(String email){         this.email = email;     }      public void setFirebaseUser(FirebaseUser firebaseUser){         this.firebaseUser = firebaseUser;       }     public FirebaseUser getFirebaseUser(){         return firebaseUser;     }      public void setLastOnline(String lastOnline){         this.lastOnline = lastOnline;     }     public String getLastOnline(){         if(lastOnline != null)             return lastOnline;          return "";     }      public void setLatLong(LatLng latlng){         this.latLng = latlng;     }     public LatLng getLatLong(){         return this.latLng;     }      public ArrayList<SwapLocation> getLocations(){         return locations;     }     public void setLocations(ArrayList<SwapLocation> locations){         this.locations = locations;     }  } 

I am trying to retrieve this object from Firebase. The object is retrieved in the dataSnapshot, although it has a problem serializing to my User object.

This is how i am retrieving the object from Firebase:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(users_group).child(user.getUid());         ref.addListenerForSingleValueEvent(             new ValueEventListener() {                 @Override                 public void onDataChange(DataSnapshot dataSnapshot) {                     User this_user = dataSnapshot.getValue(User.class);                  }                  @Override                 public void onCancelled(DatabaseError databaseError) { }             }); 

But when trying to do 'User this_user = dataSnapshot.getValue(User.class)' i get an error saying "User is missing a constructor with no arguments"

Any help would be great, thanks

like image 272
Nickmccomb Avatar asked Aug 06 '16 08:08

Nickmccomb


People also ask

What is getKey () in firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

How many users can firebase handle for free?

Firebase Realtime Database allows for 100 simultaneous connections, 1 GB stored, and 10 GB per month downloaded on the free tier.

Could not deserialize an object does not define a no argument constructor?

The error is very clear, your class "FireFran" doesn't have a no-argument constructor. When you try to deserialize an object from Cloud Firestore, the Android SDKs require that the class must have a default no-arg constructor and also setters that map to each database property.


1 Answers

I have faced this in kotlin with firebase realtime database and solve it by this solution

For Kotlin, if you're using data class like below for firebase,

data class UserModel(         var UserName: String,         var UserType: Int ) 

then you'll get an error like

com.google.firebase.database.DatabaseException: UserModel does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

So for this, you need to initialize its values.

data class UserModel(         var UserName: String = "",         var UserType: Int = 0 ) 
like image 81
Kishan Solanki Avatar answered Oct 06 '22 22:10

Kishan Solanki