I have a flow of type Boolean named selected and I...
# coroutines
t
I have a flow of type Boolean named selected and I want to get that value inside autoLocation Observer. how can I do that? , here's my code: PreferencesManager.kt
Copy code
class PreferencesManager(private val context: Context) {
                     ...
val selected : Flow<Boolean>
        get() = context.counterDataStore.data.map { preferences ->
            preferences[AUTO_LOCATION] ?: false
           }
}
LocationFragment.kt
Copy code
class LocationFragment : Fragment() {
                   ...
      lifecycleScope.launch {
            mainViewModel.autoLocation.collectLatest {
              //  check if selected      
                    it?.let {
                        mainViewModel.addPlaceItem(it)
                      }
                   }
            
          }
}
MainViewModel.kt
Copy code
class MainViewModel(
     private val application: Application
    private val placeRepository: PlaceRepository,
    private val preferencesManager: PreferencesManager,
) : ViewModel() {
                        ...
val selected = preferencesManager.selected
}
m
assuming I understood what you want to do, you could change it a bit, and instead of returning a Flow<Boolean> for selected, could be
Copy code
suspend fun readSelected (): Boolean =
         context.counterDataStore.data.map { preferences ->
            preferences[AUTO_LOCATION] ?: false
           }.first()
🙌 1
👍 1
1
t
thank you so much marios proto now it works properly
297 Views