1

I am creating a Todo App with RoomDataBase. I want to have a dropdownmenu on home screen to select which category to be shown. The category list has All, Personal, Shopping, Wishlist and Work by default. The function for fetching all todos from db is named getTodos and loads data in Flow type. In TodoListScreen the data are retrieved as State from the viewModel by collectAsState. First category on app start is set to All and it works correctly. If I try to change the category for example to Shopping, the list updates correctly. But in second try, when I try to choose another category, the list does not update. The data fetching function in repository is as below:

      override fun getTodos(category: String): Flow<List<Todo>> {
        return dao.getTodos(category)
    }

This is in viewModel:

       var todos: Flow<List<Todo>>

I took a log.d of both Flow details in viewModel and todos in TodoListScreen. I found that on category change the Flow is updated but collectAsState does not update todos in TodoListScreen.

This is in TodoListScreen

      var todos  =  viewModel.todos.collectAsState(initial = emptyList())     

And this is my lazyColumn

LazyColumn(
            modifier = Modifier.fillMaxSize()
        ) {
            items(todos.value) {todo ->
                TodoItem(
                    todo = todo,
                    onEvent = viewModel::onEvent,
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable {
                            viewModel.onEvent(TodoListEvent.OnTodoClick(todo))
                            Log.d(TAG, "injas1 ${todos.value}")

                        }
                        .padding(16.dp)
                )
            }
        }
General Grievance
  • 4,555
  • 31
  • 31
  • 45

0 Answers0