0

My ViewModel

val names =
    listOf(
        "valeria",
        "Daniela",
        "Isabella",
        "Liam",
        "Noah",
        "Jack",
        "Oliver",
        "Ava",
        "Sophia",
        "Amelia"
    )

private val _namesList = MutableStateFlow(names)
val namesList = _namesList.asStateFlow()


fun getFilteredNames(state: MutableState<TextFieldValue>) {
    viewModelScope.launch {
        val searchedText = state.value.text
        _namesList.value =
            if (searchedText.isEmpty()) {
                names
            } else {
                val resultList = ArrayList<String>()
                for (item in names) {
                    if (item.lowercase(Locale.getDefault())
                            .contains(searchedText.lowercase(Locale.getDefault()))
                    ) {
                        resultList.add(item)
                    }
                }
                Log.d("List: ", namesList.value.toString())
                resultList
            }
    }
}

Recomposition doesn't happen for some reason.

 val viewModel: MainViewModel = viewModel()
 val names = viewModel.namesList.collectAsState()
    LazyColumn(
        modifier = Modifier
            .fillMaxSize().background(MaterialTheme.colors.background)
    ) {
        items(names.value.size) {
            SearchListItem(names.value[it]) {}
        }
    }
z.g.y
  • 5,512
  • 4
  • 10
  • 36
  • I'm leaving some links to my answers regarding `SnapshotStateList` in case you haven't solve this yet. [link1](https://stackoverflow.com/questions/74134219/jetpack-compose-lazycolumn-not-updating-with-mutablelist/74134269#74134269), [link2](https://stackoverflow.com/questions/74343623/jetpack-compose-lazy-column-not-recomposing-with-list/74343738#74343738), [link3](https://stackoverflow.com/questions/74144053/lazycolumn-does-not-update-when-deleting-an-item/74144210#74144210) – z.g.y Nov 14 '22 at 01:59

2 Answers2

0

Once I had the same question (you can see it and some other answers here). if shortly, flow is updated only when you emit the new object to it (with different hashcode than the last emitted item). and you are updating the same list and then trying to pass it to flow. if you will create the copy of the list and emit this copy, everything will work ok, as it is already another object

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43
  • Log.d("List: ", namesList.hashCode().toString()) shows different hashcode each time the TextFieldValue changes but the recomposition still doesn't happen! – Noureddine Jun 26 '22 at 18:52
0

So I am not sure how are you using this callback to get more items. Simplified it would be like:

val mutableNamesList = MutableStateFlow(listOf<String>())
val namesList = mutableNamesList.asStateFlow()

@Composable
fun NamesList() {
    val coroutineScope = rememberCoroutineScope()
    var text by remember { mutableStateOf("") }
    TextField(value = text, onValueChange = { newTextValue ->
        text = newTextValue
        coroutineScope.launch {
            mutableNamesList.value = ( mutableNamesList.value + newTextValue)
        }
    })
   val list =  namesList.collectAsState().value
    LazyColumn {
        items(list) { item ->
            Text(text = item)
        }
    }
}

Just, I used this import:

import androidx.compose.foundation.lazy.items