What is more efficient mutableStateOf or mutableStateFlow?

94 Views Asked by At

I would to like to use MutableStateFlow in Compose components.

    var hasReadCode by remember { mutableStateOf(false) }
    var hasReadCodeFlow = remember { MutableStateFlow(hasReadCode) }

I have problem with mutableStateOf it is feature with QRcode reader, when it reads code this value goes with true, false, true with 100ms and it make double navigation back. So I had to make work around with flow and .debounce, but as far as I know I should use flow in viewmodels only.

    LaunchedEffect(key1 = "handleReadCode") {
        hasReadCodeFlow
            .debounce(600L)
            .collectLatest { hasReadCode ->
                if (hasReadCode) {
                    handleDataDetection(events, code, navigation)
                }
            }
    }
                QRCode { result ->
                                code = result
                                hasReadCode = true
                                hasReadCodeFlow.value = true
                            }

Can I someone improve my code? In my case I could delete mutableStateOf and stay with flow.

Greetings, Chris

0

There are 0 best solutions below