【问题标题】:Will the value of isLoading be always true in the Android Jetpack Compose project?在 Android Jetpack Compose 项目中,isLoading 的值会一直为真吗?
【发布时间】:2021-09-30 02:53:41
【问题描述】:

以下代码来自官方Advanced State in Jetpack Compose Codelab

在函数fun DetailsScreen()中,uiState.isLoading -> {...}会在isLoading为真时被触发。

我搜索了项目中的所有代码,只能找到代码val uiState by produceState(initialValue = DetailsUiState(isLoading = true))传值​​给isLoading

uiState.isLoading -> {...} 在项目中总是会被解雇吗?

data class DetailsUiState(
    val cityDetails: ExploreModel? = null,
    val isLoading: Boolean = false,
    val throwError: Boolean = false
)

@Composable
fun DetailsScreen(
    onErrorLoading: () -> Unit,
    modifier: Modifier = Modifier,
    viewModel: DetailsViewModel = viewModel()
) {
    val uiState by produceState(initialValue = DetailsUiState(isLoading = true)) {
        val cityDetailsResult = viewModel.cityDetails
        value = if (cityDetailsResult is Result.Success<ExploreModel>) {
            DetailsUiState(cityDetailsResult.data)
        } else {
            DetailsUiState(throwError = true)
        }
    }

    when {
        uiState.cityDetails != null -> {
            DetailsContent(uiState.cityDetails!!, modifier.fillMaxSize())
        }
        uiState.isLoading -> {
            Box(modifier.fillMaxSize()) {
                CircularProgressIndicator(
                    color = MaterialTheme.colors.onSurface,
                    modifier = Modifier.align(Alignment.Center)
                )
            }
        }
        else -> { onErrorLoading() }
    }
}


@HiltViewModel
class DetailsViewModel @Inject constructor(
    private val destinationsRepository: DestinationsRepository,
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    private val cityName = savedStateHandle.get<String>(KEY_ARG_DETAILS_CITY_NAME)!!

    val cityDetails: Result<ExploreModel>
        get() {
            val destination = destinationsRepository.getDestination(cityName)
            return if (destination != null) {
                Result.Success(destination)
            } else {
                Result.Error(IllegalArgumentException("City doesn't exist"))
            }
        }
}

sealed class Result<out R> {
    data class Success<out T>(val data: T) : Result<T>()
    data class Error(val exception: Exception) : Result<Nothing>()
}

【问题讨论】:

    标签: android kotlin android-jetpack-compose android-viewmodel


    【解决方案1】:

    没有

    isLoading的默认值为false

    val isLoading: Boolean = false,
    

    因此,未将其显式设置为trueDetailsUiState(throwError = true)DetailsUiState(throwError = true))的构造函数调用将导致它为false

    【讨论】:

    • 谢谢!为什么DetailsUiState(throwError = true)) 会导致isLoading 为假?
    • @HelloCW 在 Kotlin 中,未传递的参数将获取其默认值。由于只传递了throwError,所以cityDetailsisLoading得到它们的默认值,分别是nullfalse
    • 还有,我觉得业务逻辑应该是这样的,isLoadingcityDetails加载时为true,isLoadingcityDetails加载后赋值为false。但是在加载cityDetails 后,我在项目中找不到任何代码isLoading 被分配为false!
    • @HelloCW 我想我现在明白你的困惑了。其中每一个都在创建一个 DetailsUiState,而不是更改旧的。请注意,DetailsUiState 的所有字段都是 vals - 它是不可变的。
    • 谢谢!我认为uiState.isLoading 的值应该始终为false,因为DetailsUiState(cityDetailsResult.data)DetailsUiState(throwError = true) 将在此fun DetailsScreen 中默认获取值为isLoading = false 的对象,对吧?
    猜你喜欢
    • 1970-01-01
    • 2021-11-30
    • 2022-01-12
    • 2021-07-14
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2021-10-21
    相关资源
    最近更新 更多