androidandroid-jetpack-composedatastore

What could be causing Jetpack Compose data store retrieval failure?


i am using data store in jetpack compose to store string value and depending on that string i am navigating but i am not able to get any data from data store any one please help

composable(AuthDestinations.INFO){
                val context = LocalContext.current
                val pref = Preferences(context)
                var startDes = false
                val scope = rememberCoroutineScope()
                val googleAuthUiClient by lazy {
                    GoogleAuthUiClient(
                        context = context.applicationContext,
                        oneTapClient = Identity.getSignInClient(context.applicationContext)
                    )
                }
                LaunchedEffect(true){
                    startDes = pref.getData(Constraints.loginStatus)
                    //inside startDes = true
                }
             //  outside startDes = false
                Log.w("userPageStatus","$startDes,${googleAuthUiClient.getSignedInUser()}")
                if (!startDes){
                    InformationScreen(modifier = modifier) {
                        navController.navigate(AuthDestinations.LOGIN)
                        scope.launch {
                            pref.saveBooleanData(Constraints.loginStatus,true)
                        }
                    }
                }else{
                    navController.navigate(AuthDestinations.LOGIN)
                }
            }

data store code is

private val Context.dataStore : DataStore<Preferences> by preferencesDataStore("pg_rooms")
class Preferences(private val context : Context) {


    suspend fun saveBooleanData(key :  Preferences.Key<Boolean>, value : Boolean){
            context.applicationContext.dataStore.edit {preferences->
                preferences[key] = value
            }
    }

    suspend fun getData(key : Preferences.Key<Boolean>): Boolean{
        val data  = context.applicationContext.dataStore.data.first()
        return data[key] ?: false
    }
}```

Solution

  • the way you implemented Data store looks fine, the way you're updating the startDes variable, it's not mutable and not observable in compose, so you won't see the updated value in your composable immediately. so you should use the remember function to create a mutable and observable state in your composable. here is the code snippet :

    composable(AuthDestinations.INFO){
        val context = LocalContext.current
        val pref = Preferences(context)
        val startDes = remember { mutableStateOf(false) }  // creating a mutable state here
        val scope = rememberCoroutineScope()
        val googleAuthUiClient by lazy {
            GoogleAuthUiClient(
                context = context.applicationContext,
                oneTapClient = Identity.getSignInClient(context.applicationContext)
            )
        }
        LaunchedEffect(Unit){  // Pass Unit as key to make sure this effect only runs once
            startDes.value = pref.getData(Constraints.loginStatus)
        }
        Log.w("userPageStatus","${startDes.value}, ${googleAuthUiClient.getSignedInUser()}")
        if (!startDes.value){
            InformationScreen(modifier = modifier) {
                navController.navigate(AuthDestinations.LOGIN)
                scope.launch {
                    pref.saveBooleanData(Constraints.loginStatus,true)
                }
            }
        }else{
            navController.navigate(AuthDestinations.LOGIN)
        }
    }
    

    for more info about compose : State in Compose