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

How to assign value in setter of mutableStateOf in kotlin

I am working in mutableStateOf. I want to assign value when initialise the variable. I have one string value which I want to assign in mutableStateOf. When string is empty then I don’t want to set any value in my mutableStateOf variable. I am trying but getting error.

val answer: String? = null

var textFieldValue = mutableStateOf(TextFieldValue())
        set(value) {
            field = value
            if(!answer.isNullOrEmpty()){
                field = answer
            }
        }

I am getting error

Type mismatch.
Required: MutableState<TextFieldValue>
Found: String

I want to set TextFieldValue() when my answer is null. Many Thanks

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

>Solution :

This should work,

val answer: String? = null
var textFieldValue = mutableStateOf(TextFieldValue(answer ?: ""))
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