Skip to content

Instantly share code, notes, and snippets.

View AndroidPoet's full-sized avatar
God Mode

Ranbir Singh AndroidPoet

God Mode
View GitHub Profile

Kotlin Flow Cheat Sheet

Flow Types

Flow Type Purpose Key Features Use Case
flow {} Creates a cold flow that emits values lazily. Emits values only when collected.
Supports suspending functions.
Creating custom flows to emit data on demand.
flowOf() Creates a flow from a fixed set of values. Emits provided values sequentially.
Cold by nature.
Emit predefined values (e.g., configuration settings).
asFlow() Converts collections, sequences, arrays, or ranges to flows. Supports many standard types.
Cold by nature.
Convert lists or arrays into flows for processing.
`c
import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
[versions]
compose = "1.7.0-alpha01"
[libraries]
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
[plugins]
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
compose = { id = "org.jetbrains.compose", version.ref = "compose" }
//settings.gradle.kts
dependencyResolutionManagement {
repositories {
maven { url = uri("https://androidx.dev/snapshots/builds/11670047/artifacts/repository/") }
google()
mavenCentral()
maven(url = "https://plugins.gradle.org/m2/")
maven(url = "https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
//function we call in our code
@Composable
@NonRestartableComposable
@OptIn(InternalComposeApi::class)
fun LaunchedEffect(
key1: Any?,
block: suspend CoroutineScope.() -> Unit
) {
val applyContext = currentComposer.applyCoroutineContext
@Composable
fun rememberMapViewWithLifecycle() {
DisposableEffect(key1 = lifecycle, key2 = mapView) {
// Make MapView follow the current lifecycle
val lifecycleObserver = getMapLifecycleObserver(mapView)
lifecycle.addObserver(lifecycleObserver)
onDispose {
}
@Composable
fun DemoCoroutineScope() {
val scope = rememberCoroutineScope()
Button(onClick = {
scope.launch {
//CoroutineScope
//run any suspend functions inside this scope
@Composable
fun DemoLaunchedEffect(key1: String, key2: String, key3: String, list: Array<String>) {
LaunchedEffect(key1 = key1) {
//CoroutineScope
}
LaunchedEffect(key1 = key1, key2 = key2) {
//CoroutineScope
@Composable
fun TestComposable(modifier: Modifier = Modifier, data: () -> Unit) {
//scope of the composebale
//
}
@Composable
fun SecondRootComposable() {
Box(Modifier.fillMaxSize()) { // Recomposition Scope Start
val scroll = rememberScrollState(0)
Title(snack = snack, scrollProvider = {scroll.value })
}
}