asObjectsFlow
fun <T : Any> Query.asObjectsFlow(coroutineContext: CoroutineContext? = null, factory: (String) -> T?): Flow<List<T>>
Returns a Flow that maps the Query ResultSet to instances of a class that can be created using the given factory lambda.
Example of usage with kotlinx-serialization:
@Serializable
class User(
val name: String,
val surname: String,
val age: Int
)
val users: Flow<List<User>> = query.asObjectsFlow { json: String ->
Json.decodeFromString<User>(json)
}
Content copied to clipboard
Parameters
factory
the lambda used for creating object instances.
fun <T : Any> Query.asObjectsFlow(coroutineContext: CoroutineContext? = null, factory: (Map<String, Any?>) -> T?): Flow<List<T>>
Returns a Flow that maps the Query ResultSet to instances of a class that can be created using the given factory lambda.
Example of usage:
class User(map: Map<String, Any?>) {
val name: String by map
val surname: String by map
val age: Int by map
}
val users: Flow<List<User>> = query.asObjectsFlow(::User)
Content copied to clipboard
Using Kotlin Map delegation for creating such instances is a great shorthand.
Parameters
factory
the lambda used for creating object instances.