This question is related to Android app development in Kotlin. So, the thing is I want to update the username on my app (using Firebase) and save it so the updated username shows in the profile activity. When i click on edit profile, there is an edit text that gets filled with the current username and the user is able to modify it, click on save and change it. The problem comes when I try to save the edited username, it displays Kotlin.Unit
instead of the new username. I added the return statement below the function but it still displays kotlin.unit
code bellow
class EditProfile : AppCompatActivity() {
lateinit var binding: ActivityEditProfileBinding
lateinit var bindingProfileActivity: ActivityProfileBinding
lateinit var userProfileModel: UserModel
lateinit var currentUserId: String
lateinit var etUsername: String
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityEditProfileBinding.inflate(layoutInflater)
bindingProfileActivity = ActivityProfileBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(binding.root)
//firebaseAuth
currentUserId = FirebaseAuth.getInstance().currentUser?.uid!!
//get the username from the database
Firebase.firestore.collection("users")
.document(currentUserId)
.get()
.addOnSuccessListener { it ->
userProfileModel = it.toObject(UserModel::class.java)!!
//bind the username from the model to the EditText
val usernameFinal = userProfileModel.username
etUsername = binding.usernameField.setText(usernameFinal).toString()
}
//save the username when the user clicks on save
binding.savePfpButton.setOnClickListener {
saveUsername()
}//end oncreate fun
//update username method
private fun saveUsername(): String {
val updatedName: String = etUsername
val updatedProf = mapOf(
"username" to updatedName
)
//save the map in the document
Firebase.firestore.collection("users").document(currentUserId).update(updatedProf)
UiUtil.showToast(applicationContext, "Username updated")
return updatedName
}
}
}
2
Answers
So i understood what my error was and it had to do with the etusername variable. i tried what you told me but i still get the error. it retrieves the username to the textfield but when i click on save it keeps showing the kotlin.unit. The code will be like
i made the usernameFinal lateinit and global, idk if that has to do with the issue. i tried without the return statement but still get the error. i checked logcat but is not giving me anything i can work with.
You’re getting
Kotlin.Unit
instead of the new username due to the declaration of theetUsername
variable:The assignment of the data:
And the call of the
saveUsername()
function, where you use:So what you’re doing right now, you’re initializing the
etUsername
by assigning the value of what the TextView#setText() returns and then converting it toString
. Since this method returns an object of type Unit, theetUsername
field will only hold the String representation of the object which is the "Unit" String.If you want to assign to the
usernameField
TextView the updated value ofusernameFinal
, then you only have to use the following line of code:Which takes the value of
usernameFinal
that you read from Firestore and sets it to theTextView
field. So there is no need for additional fields likeetUsername
, in your activity.