Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

conditional operation with types in jetpackCompose

How to have a type to be optional and if it is not passed from outside than the second Text should not render. Right now gettin an error 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
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can give a default value of null to secondaryLabel and if it’s not null you can render that Text.

 @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,
           )
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading