@Composable
fun networkState(): State<Boolean> {
val context = LocalContext.current
return context.networkStateFlow().collectAsState(initial = false)
}
Actually I'm trying to observe the network state in composables
a
Adam Powell
08/11/2021, 1:28 PM
if
context.nextworkStateFlow()
returns a new flow instance each time (and since it looks like an extension function it probably does) you'll want to write that as
Copy code
val context = LocalContext.current
return remember(context) { context.networkStateFlow() }.collectAsState(false)
m
Mjahangiry75
08/11/2021, 1:32 PM
Thank you
could you explain why
remember
takes
context
as a parameter?
a
Adam Powell
08/11/2021, 1:37 PM
general correctness. If
LocalContext
changes you want to get a new flow for the new context. In practice you probably won't actually see this ever change. But you will see this function recompose from its caller, so you do want to remember the flow.