Sarah Brenner
07/21/2023, 6:02 PMprivate val _products = MutableStateFlow<List<Product>>(emptyList())
val products = _products.asStateFlow().cStateFlow()
Now, in iOS, I’m trying to create a list, but this doesn’t work; I’m not sure how to write it or if I’m doing it right:
List {
ForEach(homeViewModel.state(\.products), id: \.id) { product in
Text(product.title)
}
}
Is there a way to do something simple like this, or do I need to do something like this (mirror the BookList functionality)?
https://github.com/icerockdev/moko-mvvm/blob/master/sample-declarative-ui/shared/src/commonMain/kotlin/dev/icerock/moko/mvvm/sample/declarativeui/BookListViewModel.kt
I also found this pattern, where the flow is collected:
https://github.com/rvenky125/NoteKMM/blob/master/iosApp/iosApp/NoteIOSViewModel.swift
I guess my question is: what is the easiest way to loop through these values in iOS? In compose, I just do this, and access the value:
val products = viewModel.products.collectAsStateWithLifecycle()
Sarah Brenner
07/21/2023, 11:51 PMList {
ForEach(homeViewModel.state(\.products, equals: { $0 === $1 },
mapper: { ($0 as! [Product]?)! }), id: \.id) { product in
Text(product.title)
}
}
I am still curious about best practice though, and if anyone has some examples, I’d love to check them out.alex009
07/24/2023, 3:10 AMstate(:equals:mapper:)
function. to improve usability we create extensions with this calls - https://github.com/icerockdev/moko-mvvm/blob/80b6e45d1d007188087ad0bdd8aa15b032e50[…]1/sample-declarative-ui/iosApp/iosApp/BookListViewBinding.swiftSarah Brenner
07/24/2023, 3:17 AMalex009
07/24/2023, 3:18 AM