Firebase “User is missing a constructor with no arguments”

后端 未结 4 2068
不知归路
不知归路 2020-12-14 18:28

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          


        
相关标签:
4条回答
  • 2020-12-14 18:58

    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
    )
    
    0 讨论(0)
  • 2020-12-14 19:00

    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!

    0 讨论(0)
  • 2020-12-14 19:03

    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=""
    )
    
    0 讨论(0)
  • 2020-12-14 19:07

    You must be getting this error because your User class is a sub class.

    Solution:

    Either mark your class static:

     public class SomeParentClass{ 
    
            public static class User {
                ....
            }
        }
    

    or create it in a separate file.

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