Home > Blockchain >  Text is not updating even though I am using remember and mutableStateOf
Text is not updating even though I am using remember and mutableStateOf

Time:12-11

I've run into a strange issue in Jetpack Compose. I am currently working on an app which shows data on a graph from an external API. I am fetching data with no problem, but my graph didn't recompose with new values.

After debugging, I started to question composition within my app. For debugging purposes, I created a simplest possible composable function to see if everything is working properly.

And it doesn't work at all... I tried code below in different project and everything is fine there.

@Composable
fun Counter() {
    val count = remember { mutableStateOf(0) }
    val context = LocalContext.current
    Column(
        Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Button(
            onClick = {
                count.value  
                Log.d("f", "Value: ${count.value}")
                Toast.makeText(context, "Clicked button ${count.value}", Toast.LENGTH_SHORT).show()
            }
        ) {
            Text("Click me")
        }
        Text(text = "Current count: ${count.value}")
    }
}

I can see count value increasing in Logcat as well as in the Toast, however the Text stubbornly shows only 0.

Here are the screenshots: from emulator from the Logcat

Here is my build.gradle file (Note that compose_ui_version is set to '1.3.1'.):

plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.example.airmockapiapp'
compileSdk 33

defaultConfig {
    applicationId "com.example.airmockapiapp"
    minSdk 30
    targetSdk 33
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary true
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion compose_ui_version
}
packagingOptions {
    resources {
        excludes  = '/META-INF/{AL2.0,LGPL2.1}'
    }
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.6.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.3"
implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.3"

// Compose viewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"

// Compose navigation
implementation "androidx.navigation:navigation-compose:2.5.3"

//Jetpack Compose Charts from tehras
//implementation "com.github.tehras:charts:beta-01"

// Williamchart xml charts library
//implementation 'com.diogobernardino:williamchart:3.3.0'

// Composable-Graphs from jaikeerthick github
implementation 'com.github.jaikeerthick:Composable-Graphs:v1.1'

}

CodePudding user response:

There are conflicting versions of some compose libraries in your configuration. To fix that issue in your case you can manually change version of androidx.compose.material:material to 1.3.1. To avoid such problems in the future, it'll be better to use bom files - it's official recommendation.

  • Related