在可变状态变量中,Android在新状态后不更新

提问

我正面临着喷气背包组成的可变状态的零星问题。在viewModel中,我有一个类型数据类的变量。 当我更新viewModel中的变量时 更新客户机和报告号的状态被更新,但看起来UI没有接收新的状态信号来更新UI,而重新组合并没有发生。 本节使用国家: 标题定义为: ModelTemplateHeaderUiModel定义为: 最奇怪的是,那些更新不被听到的变量只有3个: 合同编号 报告号 和客户 描述和日期在viewModel中正确更新: 因此,更新对象状态的方法没有区别。 我通过调试器进行了一些检查,实际上从firebase到变量的数据都正确地加载了状态,但是新的数据没有显示出来。我不明白同样的更新数据方法是如何为一些变量而不是为其他变量而工作的。我想澄清的是,没有奇怪的逻辑来操纵未显示的数据。所有的数据都是直接到UI而不是操作。 我在使用 有人有什么建议吗?谢谢

var headerState by mutableStateOf(ModelTemplateHeaderUiModel())
    private set

headerState = headerState.copy(
            client = RepoManager.getClientById(clientID) ?: Client(),
            reportNumber = (RepoManager.getLastReportNumberByClient(clientID))?.plus(1)
                ?.toString()
                ?: ""
        )

 Header(
            viewModel.headerState,
            onContractNumberChange = { viewModel.updateContractNumber(it) },
            onReportNumberChange = { viewModel.updateReportNumber(it) },
            onDateChange = { viewModel.updateDate(it) },
        )

@Composable
fun Header(
    headerState: ModelTemplateHeaderUiModel = ModelTemplateHeaderUiModel(),
    onContractNumberChange: (String) -> Unit,
    onReportNumberChange: (String) -> Unit,
    onDateChange: (LocalDate) -> Unit,
)

@Parcelize
data class ModelTemplateHeaderUiModel(
    var descriptionHeader: String = "",
    var client: Client = Client(),
    var contractNumber: String = "",
    var reportNumber: String = "",
    var date: Long = Clock.System.now().epochSeconds
) : Parcelable

OutlinedTextField(
                    modifier = Modifier.fillMaxWidth(),
                    value = headerState.contractNumber,
                    onValueChange = { onContractNumberChange(it) },
                    singleLine = true,
                    placeholder = { Text(text = "Contratto n掳", color = Color.Gray) },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                label = { Text(text = "Contratto n掳") }
            )

OutlinedTextField(
                    modifier = Modifier.fillMaxWidth(),
                    value = headerState.reportNumber, onValueChange = { onReportNumberChange(it) },
                    singleLine = true,
                    placeholder = { Text(text = "Rapporto di lavoro n掳", color = Color.Gray) },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    label = { Text(text = "Rapporto di lavoro n掳") }
                )

Column(
                verticalArrangement = Arrangement.spacedBy(10.dp),
                modifier = Modifier.weight(1f)
            ) {
                Text(text = "Cliente", style = MaterialTheme.typography.titleLarge)
                Text(text = headerState.client.name, style = MaterialTheme.typography.titleMedium)
                Text(text = headerState.client.address)
                Row(
                    horizontalArrangement = Arrangement.spacedBy(10.dp),
                ) {
                    Text(text = headerState.client.cap)
                    Text(text = headerState.client.city)
                    Text(text = headerState.client.province)
                }
            }

fun loadModelById(modelID: String) {
        viewModelScope.launch {
            modelState.value = RepoManager.getCompleteModelById(modelID) ?: Model()
            //update components
            components.clear()
            components.addAll(modelState.value.components)
            //update header state
            headerState = headerState.copy(descriptionHeader = modelState.value.headerDescription)
        }
    }
fun updateDate(value: LocalDate) {
        headerState =
            headerState.copy(date = value.atStartOfDayIn(TimeZone.currentSystemDefault()).epochSeconds)
    }

implementation 'androidx.core:core-ktx:1.8.0'
    implementation "androidx.compose.ui:ui:1.3.0-alpha01"
    implementation 'androidx.compose.compiler:compiler:1.2.0'
    implementation 'androidx.compose.material3:material3:1.0.0-alpha14'
    implementation "androidx.compose.ui:ui-tooling-preview:1.3.0-alpha01"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
    implementation 'androidx.activity:activity-compose:1.5.0'
    implementation 'androidx.navigation:navigation-runtime-ktx:2.5.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.3.0-alpha01"
    debugImplementation "androidx.compose.ui:ui-tooling:1.3.0-alpha01"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.3.0-alpha01"
minSdk 26
targetSdk 32
 jvmTarget = '1.8'
compose_version = '1.2.0'
plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}

▼版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说cuoshuo.com——程序员的报错记录

部分内容根据CC版权协议转载,如果您希望取消转载请发送邮件到cuoshuo8@163.com

辽ICP备19011660号-5