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
public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.
Firebase Realtime Database allows for 100 simultaneous connections, 1 GB stored, and 10 GB per month downloaded on the free tier.
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.
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With