Firebase “User is missing a constructor with no arguments”

后端 未结 4 2083
不知归路
不知归路 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 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 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? = null
    )
    

    Hope it helps!

提交回复
热议问题