cometchat implementation in kotlin

76 Views Asked by At

I asked a question on integrating cometchat into my application. i didnt get any reply but i will continue to ask unti i solve my problem as i am very new to kotlin. Following the instruction from the cometchat website on how to register my user i added the following code to my Main activity

val authKey = "AUTH_KEY" // Replace with your App Auth Key
    val user = User()
    user.getUid() = FirebaseAuth.getInstance().currentUser!!.uid // Replace with the UID for the user to be created
    user.getfullname() = full_name.toString() // Replace with the name of the user

    CometChat.createUser(user, authKey, object : CometChat.CallbackListener<com.cometchat.pro.models.User>() {
        override fun onSuccess(user: User) {
            Log.d("createUser", user.toString()
        }

        override fun onError(e: CometChatException) {
            Log.e("createUser", e.message)
        }
    })

Unfortunately, user.getUid is underlined in red saying "variable expected". I have no idea what that means. This is my user Object

package Model

class User {
private var stOforigin: String = ""
private  var fullname:String =""
private var bio:String =""
private var image:String =""
private var uid:String =""
private var gender:String =""
private var email:String =""

constructor()

constructor(fullname:String, bio:String, image:String, uid:String, gender:String , sos:String, 
email:String){
    this.fullname = fullname
    this.bio = bio
    this.image= image
    this.uid = uid
    this.stOforigin = sos
    this.gender =gender
    this.email = email
}

fun getfullname(s: String): String{
    return fullname
}
fun setfullname(fullname: String){
    this.fullname= fullname
}
fun getBio(): String{
    return bio
}
fun setBio(bio: String){
    this.bio= bio
}
fun getImage(): String{
    return image
}
fun setImage(image: String){
    this.image= image
}
fun getUid(uid: Any?): String{
    return this.uid
}
fun setUid(uid: String){
    this.uid= uid
}
fun getGender(): String{
    return gender
}
fun setGender(gender: String){
    this.gender= gender
}

fun getstOforigin(): String{
    return stOforigin
}
fun setstOforigin(stOforigin: String){
    this.stOforigin= stOforigin
}
fun getEmail(): String{
    return email
}
fun setEmail(email: String){
    this.email = email
}

}

1

There are 1 best solutions below

1
On

You cannot assign a value to a function call. In other words, foo() = 123 is invalid syntax, regardless of what foo() does. In your case, rather than calling your getter-style functions (get...()), call the setter-style functions (set...()), passing your desired value as a parameter.

Replace:

    user.getUid() = FirebaseAuth.getInstance().currentUser!!.uid // Replace with the UID for the user to be created
    user.getfullname() = full_name.toString() // Replace with the name of the user

with:

    user.setUid(FirebaseAuth.getInstance().currentUser!!.uid) // Replace with the UID for the user to be created
    user.setfullname(full_name.toString()) // Replace with the name of the user

You might wish to put all of this aside for a while and focus on learning Kotlin first. If it helps, here is a free book of mine on Kotlin.