如何使类型是可选的,如果它不是从外部传递的,则不应呈现第二个文本。现在获取一个错误Type mismatch: inferred type is String but Boolean was expected
 @Composable
    fun FieldLabel(
        label: String,
        secondaryLabel: String?,
        modifier: Modifier = Modifier,
    
    ) {
        Text(
            text = label,
            textAlign = TextAlign.End,
            modifier = Modifier
                .fillMaxWidth(),
            fontWeight = FontWeight.Bold,
            fontSize = 24.sp,
            color = Color.Black,
        )
//How to write this part so that if there is not secondaryLabel provided than the text part does not render
       secondaryLabel ? Text(
            text = secondaryLabel,
            modifier = Modifier
                .fillMaxWidth()
            fontWeight = FontWeight.Bold,
            fontSize = 24.sp,
            color = Color.Black,
        ) : null
    }发布于 2022-10-08 07:22:15
您可以将null的默认值赋给secondaryLabel,如果它不是null,则可以呈现该文本。
 @Composable
    fun FieldLabel(
        label: String,
        secondaryLabel: String? = null,
        modifier: Modifier = Modifier,
    
    ) {
        Text(
            text = label,
            textAlign = TextAlign.End,
            modifier = Modifier
                .fillMaxWidth(),
            fontWeight = FontWeight.Bold,
            fontSize = 24.sp,
            color = Color.Black,
        )
       if(secondaryLabel != null)
           Text(
               text = secondaryLabel,
               modifier = Modifier
                .fillMaxWidth()
               fontWeight = FontWeight.Bold,
               fontSize = 24.sp,
               color = Color.Black,
           )
    }https://stackoverflow.com/questions/73995112
复制相似问题