question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing `lifecycle` parameter for `observe` function (jetbrains-compose.v020-build128)

See original GitHub issue

This is the decompose setup I use (following the guide in the readme):

import androidx.compose.foundation.layout.Column
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import com.arkivanov.decompose.value.MutableValue
import com.arkivanov.decompose.value.Value
import com.arkivanov.decompose.value.observe

class WithCharacterState {
    private val _value = MutableValue(mutableListOf<Character>())
    val state: Value<List<Character>> = _value

    fun addCharacter(character: Character) {
        _value.value.add(character)
    }
}

@Composable
fun WithCharacterState.render() {
    state.observe { state ->
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = state.toString())

            Button(onClick = { addCharacter(CharacterTemplate.DEFAULT_CHARACTER)} ) {
                Text("Add Character")
            }
        }
    }
}

However I receive the following two errors:

e: /xetra11/app/module/character/WithCharacterState.kt: (25, 11): No value passed for parameter 'lifecycle'
e: /xetra11/app/module/character/WithCharacterState.kt: (26, 9): @Composable invocations can only happen from the context of a @Composable function

my build.gradle.kts:

import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.20"
    id("org.jetbrains.compose") version "0.2.0-build128"
}

group = "com.github.xetra11"
version = "0.0.1"

repositories {
    jcenter()
    mavenCentral()
    google()
    maven { url = uri("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
    maven { url = uri("https://dl.bintray.com/arkivanov/maven") }
}

dependencies {
    implementation(compose.desktop.currentOs)
    implementation("org.slf4j:slf4j-simple:2.0.0-alpha1")

    val decomposeVersion = "0.1.2"
    implementation("com.arkivanov.decompose:decompose:$decomposeVersion")
    implementation("com.arkivanov.decompose:extensions-compose-jetbrains:$decomposeVersion")

    testImplementation(kotlin("test-junit5"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
    testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
    testImplementation("org.assertj:assertj-core:3.18.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "11"
}

compose.desktop {
    application {
        mainClass = "MainKt"
        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "app"
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
arkivanovcommented, Nov 25, 2020

Also you may like to use Value.asState extension function:

@Composable
fun WithCharacterState.render() {
    val value by state.asState()

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        Text(text = value.toString())

        Button(onClick = { addCharacter(CharacterTemplate.DEFAULT_CHARACTER)} ) {
            Text("Add Character")
        }
    }
}

Probably it is worth to deprecate the Composable Value.observe extension, will think about it.

0reactions
xetra11commented, Nov 25, 2020

@arkivanov Thanks for your time but I think I mixed up some concepts here and there. Having a hard time grasping the state concepts with Compose for Desktop. Completely new to the field 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Side-effects in Compose - Android Developers
A side-effect is a change to the state of the app that happens outside the scope of a composable function. Due to composables'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found