Jan
04/27/2022, 2:17 PMprivate val products: SnapshotStateMap<String, List<ShoppingItem>> = mutableStateMapOf()
fun add(shop: String, product: ShoppingItem) {
val list = products.getOrDefault(shop, mutableListOf()).toMutableList()
list.add(product)
products[shop] = list
}
fun replace(shop: String, product: ShoppingItem) {
val list = products.getOrDefault(shop, mutableListOf()).toMutableList()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
list.replaceAll { if (it.id == product.id) product else it }
}
products[shop] = list
}
I mean adding works but the replace method doesn't wanna work and I don't think thats the right wayhfhbd
04/27/2022, 2:20 PMState
. Instead adding a new product the inner list, you should update the entry of products
with a readonly listJan
04/27/2022, 2:22 PMproducts[shop] = list.toList()
Jan
04/27/2022, 2:25 PMproducts[shop] = products.getOrDefault(shop, listOf()).plus(product)
hfhbd
04/27/2022, 2:28 PMJan
04/27/2022, 2:28 PMJan
04/27/2022, 2:29 PMJan
04/27/2022, 3:12 PMhfhbd
04/27/2022, 3:25 PMJan
04/27/2022, 3:38 PMJan
04/27/2022, 4:20 PM