2

I'm trying to get data from server and cache into database and return new fetched list to user. I'm getting response form server and saving it to the local database but when im trying to observer it from composable function it showing list is empty.

When i try to debug and collect flow data in myViewModel class it showing but it not showing is composable function.

dao

@Dao
interface CategoryDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(categories: List<Category>)

    @Query("SELECT * FROM categories ORDER BY name")
    fun read(): Flow<List<Category>>

    @Query("DELETE FROM categories")
    suspend fun clearAll()
}

repository class:

    suspend fun getCategories(): Flow<List<Category>> {
        val categories = RetrofitModule.getCategories().categories
        dao.insert(categories)
        return dao.read()
    }

myViewModel

    fun categoriesList(): Flow<List<Category>> {
        var list: Flow<List<Category>> = MutableStateFlow(emptyList())
        viewModelScope.launch {
            list = repository.getCategories().flowOn(Dispatchers.IO)
        }
        return list
    }

Observing from:

@Composable
fun StoreScreen(navController: NavController, viewModel: CategoryViewModel) {
    val list = viewModel.categoriesList().collectAsState(emptyList())
    Log.d("appDebug", list.value.toString()) // Showing always emptyList []
}

current response :

2021-05-15 16:08:56.017 5125-5125/com.demo.app D/appDebug: []
General Grievance
  • 4,555
  • 31
  • 31
  • 45

1 Answers1

4

You are never updating the value of MutableStateFlow which has been collected as state in the Composable function.

Also you are assigning a Flow type object to a MutableStateFlow variable.

We can just update the value of the collected flow in the compose using:-

mutableFlow.value = newValue

We need to change the type of list to MutableStateFlow<List<Category>> instead of Flow<List<Category>>

Try this:-

 var list: MutableStateFlow<List<Category>> = MutableStateFlow(emptyList()) // changed the type of list to mutableStateFlow
 viewModelScope.launch {
    repository.getCategories().flowOn(Dispatchers.IO).collect { it ->
         list.value = it
    }
 }
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • Thanks for your response. But list.value isn't showing –  May 15 '21 at 11:22
  • please check my full answer , it will show when we initialize `list` using `var list: MutableStateFlow> = MutableStateFlow(emptyList())` @Jonas – Santanu Sur May 15 '21 at 11:23