skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    private fun saveUsername():String {
                val editedPfp:String = selectedImgUri.toString()
                val updatedProf = mapOf(
                    "username" to usernameFinal,
                    "profilePic" to editedPfp
                )
                Firebase.firestore.collection("users").document(currentUserId).update(updatedProf)
                UiUtil.showToast(applicationContext, "Username updated")
        return usernameFinal
    }
    

    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.


  2. The problem comes when I try to save the edited username, it displays Kotlin.Unit instead of the new username.

    You’re getting Kotlin.Unit instead of the new username due to the declaration of the etUsername variable:

    lateinit var etUsername: String
    

    The assignment of the data:

    etUsername = binding.usernameField.setText(usernameFinal).toString()
    

    And the call of the saveUsername() function, where you use:

    val updatedName: String = etUsername
    

    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 to String. Since this method returns an object of type Unit, the etUsername 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 of usernameFinal, then you only have to use the following line of code:

    binding.usernameField.setText(usernameFinal)
    

    Which takes the value of usernameFinal that you read from Firestore and sets it to the TextView field. So there is no need for additional fields like etUsername, in your activity.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search