Search code examples
androidkotlinandroid-jetpack-composecompose-multiplatform

Better way to store by remember mutableStateOf object in Compose?


I need to store a var which is a File. I tried these options:

var selectedFolder by remember { mutableStateOf(null) }
var selectedFolder by remember { mutableStateOf(File()) }

None of them works, both give compiler exceptions. The first one gives:

Type mismatch.
Required:
Nothing?
Found:
File

and the second one gives:

Property delegate must have a 'getValue(Nothing?, KProperty*>)' method. None of the following functions are suitable.

What is the correct way to store a object in a by remember var in Compose?


Solution

  • You can define selectedFolder variable like this.

    var selectedFolder by remember { mutableStateOf<File?>(null) }