How to emit Flow value from different function? Kotlin Coroutines

You can use StateFlow or SharedFlow APIs for such use case. Here’s a sample code with the usage of StateFlow. import kotlinx.coroutines.* import kotlinx.coroutines.flow.* val chatFlow = MutableStateFlow<String>(“”) fun main() = runBlocking { // Observe values val job = launch { chatFlow.collect { print(“$it “) } } // Change values arrayOf(“Hey”, “Hi”, “Hello”).forEach { delay(100) … Read more

When and how to use Result in Kotlin?

TL;DR: never in business code (prefer custom sealed classes), but you can consider Result if you build a framework that must relay all kinds of errors through a different way than the exception mechanism (e.g. kotlinx.coroutines’s implementation). First, there is actually a list of use cases for the motivation of the initial introduction of Result, … Read more

Why doesn’t Kotlin allow you to use lateinit with primitive types?

For (non-nullable) object types, Kotlin uses the null value to mark that a lateinit property has not been initialized and to throw the appropriate exception when the property is accessed. For primitive types, there is no such value, so there is no way to mark a property as non-initialized and to provide the diagnostics that … Read more

Boilerplate project configuration in Gradle with Gradle Kotlin DSL

If you want to benefit from all the Gradle Kotlin DSL goodness you should strive to apply all plugins using the plugins {} block. See https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md You can manage plugin repositories and resolution strategies (e.g. their version) in your settings files. Starting with Gradle 4.4 this file can be written using the Kotlin DSL, aka … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)