Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LiveData as state inside Jetpack @Compose functions

I want to use a LiveData<List<DataClass>> to be the source of my state in a @Composable function.

I cannot use the new @Model annotation, I have seen in this talk Link(at 32:06) it is possible to use LiveData, Flow, etc. by invoking the function +observe(/* Data */).

To the problem: I cannot find the function used in the video (+observe()) or any other way to use LiveData as an origin. How can I use LiveData inside my @Compose function?

Project Gradle:

buildscript {
    ext.kotlin_version = '1.3.60-eap-76'
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0-alpha04'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

App gradle: Dependencies:

   def lifecycle_version = "2.1.0"
   def compose_version = "0.1.0-dev02"

    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"

    implementation "androidx.compose:compose-runtime:$compose_version"
    kapt "androidx.compose:compose-compiler:$compose_version"

    // Android Compose
    implementation "androidx.ui:ui-layout:$compose_version"
    implementation "androidx.ui:ui-foundation:$compose_version"
    implementation "androidx.ui:ui-framework:$compose_version"
    implementation "androidx.ui:ui-tooling:$compose_version"
    implementation "androidx.ui:ui-android-text:$compose_version"
    implementation "androidx.ui:ui-text:$compose_version"
    implementation "androidx.ui:ui-material:$compose_version"


like image 997
2jan222 Avatar asked Nov 28 '19 21:11

2jan222


People also ask

How do you observe state in jetpack compose?

The general pattern for state hoisting in Jetpack Compose is to replace the state variable with two parameters: value: T : the current value to display. onValueChange: (T) -> Unit : an event that requests the value to change, where T is the proposed new value.

How do you use Viewmodels in jetpack compose?

There are two ways to declare the data within a ViewModel so that it is observable. One option is to use the Compose state mechanism which has been used extensively throughout this book. An alternative approach is to use the Jetpack LiveData component, a topic that will be covered later in this chapter.

What is recomposition in jetpack compose?

Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.

How do you observe LiveData?

Attach the Observer object to the LiveData object using the observe() method. The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.


Video Answer


2 Answers

Here is another way to observe live data using state. There is an extension function for this just include it.

add gradle dependency below one:

implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'

now just convert your regular LiveData to State for example.

 val breedItems by doggoViewModel.liveBreedData().observeAsState()
like image 98
vikas kumar Avatar answered Oct 17 '22 11:10

vikas kumar


As some already pointed out you need to add the new dependency to build.gradle

implementation 'androidx.compose.runtime:runtime-livedata:1.0.0-beta01'

And then simply:

val name: String by viewModel.name.observeAsState("")

If this does not work for you try the next below. The above syntax is clearly the better one and is also recommended but did not work for me.

val nameState: State<String> = viewModel.name.observeAsState("")
nameState.value

See also the documentation about state here https://developer.android.com/jetpack/compose/state#viewmodel-state

like image 15
ksysha Avatar answered Oct 17 '22 13:10

ksysha