How can I serialize my User class for the Firebase Database and fix this error?

前端 未结 2 800
情话喂你
情话喂你 2020-11-27 07:46

I\'m trying to retrieve the updated User from the dataSnapshot of the Firebase Database:

mRef.addValueEventListener(new ValueEventListener() {
        @Over         


        
相关标签:
2条回答
  • 2020-11-27 08:27

    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 just need to initialize its values.

    data class UserModel(
            var userName: String = "",
            var userType: Int = 0
    )
    
    0 讨论(0)
  • 2020-11-27 08:32

    Create a constructor with no arguments as

    public User() { } as cricket mentioned and to serialize

    public class User implements Serializable {
    
    private String firstName;
    private String lastName;
    private String profileId;
    
    public User() {
    }
    
    public User(String fName, String lName, String id) {
        this.firstName = fName;
        this.lastName = lName;
        this.profileId = id;
    
    }
    public String getProfileId() {
        return profileId;
    }
    
    public void setProfileId(String id) {
        this.profileId = id;
    }
    
    public String getLastName() {
        return lastName;
    }
    
    public void setLastName(String lName) {
        this.lastName = lName;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String fName) {
        this.firstName = fName;
    }
    

    }

    If this didn't meet requirement note down

    0 讨论(0)
提交回复
热议问题