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
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
)
The User has no arguments error means that even though you do need an empty constructor:
public User(){ }
You will also need another constructor with the variables that are children in your object in Firebase inside of the constructor. For instance:
public User(
String name,
String email,
FirebaseUser firebaseUser,
String lastOnline,
LatLng latLng,
ArrayList<SwapLocation> locations
){
this.name = name;
this.email = email;
this.firebaseUser = firebaseUser;
this.lastOnline = lastOnline;
this.latLong = latLong;
this.locations = locations;
}
Also be careful with classes inside of the constructors such as SwapLocation
, if it is not automatically parsed, you need to modify it the same way.
In the other hand, in Kotlin you should not instantiate with empty values, instead you should instantiate with null and add @IgnoreExtraProperties
and @Keep
on top to load attributes only if they exist in the data. For instance:
@Keep
@IgnoreExtraProperties
open class User(
var name : String? = null,
var email : String? = null,
var firebaseUser : FirebaseUser? = null,
var lastOnline : String? = null,
var latLng : LatLng? = null,
var locations : ArrayList<SwapLocation>? = null
)
Hope it helps!
in kotlin it is need to initialized data class contructor variable to dignose this error.
data class Images(val imageOne:String="",
val imageTwo:String="",
val imageThree:String=""
)
You must be getting this error because your User class is a sub class.
Either mark your class static:
public class SomeParentClass{
public static class User {
....
}
}
or create it in a separate file.