android 要求“< FirebaseUser>?"?

zbq4xfa0  于 2023-01-03  发布在  Android
关注(0)|答案(1)|浏览(74)

我尝试使用firebase创建一个livedata,但由于某种原因,我在使用此语句"_loginFlow. value = result"时收到一个错误-它说:"要求:FirebaseUser〉?,找到:Firebase用户〉
是什么导致的?任何帮助都是感激的!
我的代码:

private val _loginFlow = MutableStateFlow<Resource<FirebaseUser>?>(null)
val loginFlow: StateFlow<Resource<FirebaseUser>?> = _loginFlow


    fun loginUser(email: String, password: String) = viewModelScope.launch {
    _loginFlow.value = Resource.Loading
    val result = repository.login(email, password)
    _loginFlow.value = result
}
@Composable
fun LoginButton(onClick: () -> Unit) {

   // LOGIN BUTTON
 Spacer(modifier = Modifier.height(20.dp))
        Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
                        Button(
                            onClick = {
                                viewModel?.loginUser(email, password)
                            },
                            shape = RoundedCornerShape(50.dp),
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(50.dp)
                        ) {
                            Text(text = "Login")
                        }
                    }

验证存储库:

interface AuthRepository {
    val currentUser: FirebaseUser?
    suspend fun login(email: String, password: String): Resource<FirebaseUser>
    suspend fun signup(name: String, email: String, password: String): Resource<FirebaseUser>
    fun logout()
}

资源:

sealed class Resource<out R> {
    data class Success<out R>(val result: R) : Resource<R>()
    data class Failure(val exception: Exception) : Resource<Nothing>()
    object Loading : Resource<Nothing>()
}
atmip9wb

atmip9wb1#

首先,你不需要在MutableStateFlow〈Resource?〉中添加'?',因为Resource不应该为null,它总是你在里面定义的值之一,所以只需要删除它,然后检查。

相关问题