Orta seviye
19 Ağustos 2026Hatayı bul: Android
Tek bir kod parçası. Hatayı bul ya da hata olmadığını kanıtla.
kotlin
1import androidx.lifecycle.ViewModel
2import kotlinx.coroutines.GlobalScope
3import kotlinx.coroutines.flow.MutableStateFlow
4import kotlinx.coroutines.flow.StateFlow
5import kotlinx.coroutines.flow.asStateFlow
6import kotlinx.coroutines.launch
7
8class UserViewModel(private val repository: UserRepository) : ViewModel() {
9
10 private val _user = MutableStateFlow<User?>(null)
11 val user: StateFlow<User?> = _user.asStateFlow()
12
13 fun loadUser(id: String) {
14 GlobalScope.launch {
15 _user.value = repository.fetchUser(id)
16 }
17 }
18}Soruna yol açan satırlara tıkla.
Kararın
Günde bir deneme. Cevap hemen ardından görünür.
Serini koru
Serini korumak ve ilerlemeni takip etmek için hesap oluştur.