mapSaver
Function
Common
public fun <T> mapSaver(
save: SaverScope.(value: T) -> Map<String, Any?>,
restore: (Map<String, Any?>) -> T?,
): Saver<T, Any>
The Saver
implementation which allows to represent your class as a map of values which can be
saved individually.
What types can be saved is defined by SaveableStateRegistry
, by default everything which can be
stored in the Bundle class can be saved.
You can use it as a parameter for rememberSaveable
.
Code Examples
MapSaverSample
@Composable
fun MapSaverSample() {
data class User(val name: String, val age: Int)
val userSaver = run {
val nameKey = "Name"
val ageKey = "Age"
mapSaver(
save = { mapOf(nameKey to it.name, ageKey to it.age) },
restore = { User(it[nameKey] as String, it[ageKey] as Int) },
)
}
}