Hello, is there any best practice to return a
State<T>
AND a
StateFlow<T>
, backed by the same changing data, at the same time from an Android ViewModel? I have some View based UI which consumes the data as
StateFlow
, and some Compose UI, which would need
State
. And ideally I don’t want to rewrite the Compose part to
collectAsState()
, rather just have it consume State directly from ViewModel (e.g. hide the implementation details inside ViewModel):
Inside ViewModel:
// is consumed by View based UI
protected val _itemsFlow = MutableStateFlow<List<Item>>(emptyList())
val itemsFlow = _itemsFlow.asStateFlow()
// should be consumed by Compose UI
val items: State<List<Item>> = itemsFlow.collectAsState() // not working bc. of missing @Composable scope
val items: State<List<Item>> = itemsFlow.collectAsState(emptyList()) // also not working