Update a variable in companion object

Viewed 24

I want to know if there is a way to update a variable value created in a companion object inside a viewModel. Here is my code

        @HiltViewModel
    class ApiViewModel @Inject constructor(
        application: Application,
        private val platformRepository: PlatformRepository,
    )

     : AndroidViewModel(application) {
         companion object {
            var userProfile: MutableState<Person> = mutableStateOf(Person())
      }
fun getUserProfile() {
        viewModelScope.launch {
            try {
                val response = platformRepository.getUserProfile()
                if (response.isSuccessful) {
                    userProfile.value = response.body()!!
                } else {
                    Log.d("Error", "Code: ${response.code()}, message: ${response.errorBody()}")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
    }

The variable is not updating its value after calling the getUserProfile(). Am I missing something? Thank you so much.

0 Answers
Related