My UI gets set before my code can fetch the data for the Text that should be displayed, after rotating the tablet it calls a recompostion and it shows

as stated in the title im having problems with my ui and the text it should display. im working inside a kotlin file and created this composable:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Composable
fun Home(navController: NavHostController) {
val viewModel: HomeViewModel = viewModel()
val firmwareInfoState by viewModel.getFirmwareInfo().collectAsState()
Column {
firmwareInfoState?.let { info ->
Text(text = info)
}
}
}
</code>
<code>@Composable fun Home(navController: NavHostController) { val viewModel: HomeViewModel = viewModel() val firmwareInfoState by viewModel.getFirmwareInfo().collectAsState() Column { firmwareInfoState?.let { info -> Text(text = info) } } } </code>
@Composable
fun Home(navController: NavHostController) {
    val viewModel: HomeViewModel = viewModel()

    val firmwareInfoState by viewModel.getFirmwareInfo().collectAsState()

    Column {
        firmwareInfoState?.let { info ->
            Text(text = info)
        }
    }
}

and this the view model im using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class HomeViewModel : ViewModel() {
private val _firmwareInfo = MutableStateFlow<String?>(null)
fun getFirmwareInfo(): StateFlow<String?> {
viewModelScope.launch {
val firmware = dataloggerViewModel.getFIRMWARE()
_firmwareInfo.value = firmware
}
return _firmwareInfo
}
}
</code>
<code>class HomeViewModel : ViewModel() { private val _firmwareInfo = MutableStateFlow<String?>(null) fun getFirmwareInfo(): StateFlow<String?> { viewModelScope.launch { val firmware = dataloggerViewModel.getFIRMWARE() _firmwareInfo.value = firmware } return _firmwareInfo } } </code>
class HomeViewModel : ViewModel() {
    private val _firmwareInfo = MutableStateFlow<String?>(null)

    fun getFirmwareInfo(): StateFlow<String?> {
        viewModelScope.launch {
            val firmware = dataloggerViewModel.getFIRMWARE()
            _firmwareInfo.value = firmware
        }
        return _firmwareInfo
    }
}

i swapped from my “ConnectionScreen” to the “HomeScreen” with this Composable:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Composable
fun NavigationButton(navController: NavHostController) {
val context = LocalContext.current
var baudRate = BaudRate()
var test = 1200000
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(65.dp))
Button(
onClick = {
dataloggerViewModel.open(getUsbSerialPort(context))
navController.navigate(Screen.HomeScreen.route)
}
) {
Text(text = "connect to available dk-logger")
}
}
}
</code>
<code>@Composable fun NavigationButton(navController: NavHostController) { val context = LocalContext.current var baudRate = BaudRate() var test = 1200000 Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Spacer(modifier = Modifier.height(65.dp)) Button( onClick = { dataloggerViewModel.open(getUsbSerialPort(context)) navController.navigate(Screen.HomeScreen.route) } ) { Text(text = "connect to available dk-logger") } } } </code>
@Composable
fun NavigationButton(navController: NavHostController) {
    val context = LocalContext.current
    var baudRate = BaudRate()
    var test = 1200000

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(65.dp))
        Button(
            onClick = {
                dataloggerViewModel.open(getUsbSerialPort(context))
                navController.navigate(Screen.HomeScreen.route)
            }
        ) {
            Text(text = "connect to available dk-logger")
        }
    }
}

Normally you would expect the Text(text =””) to directly display the info but for some reason it happens when im rotating the screen. What that indicates is that when the recompostion gets scheduled, the info is there but not when the ui gets initially built. Due to the fact that it is my first time using State, LiveData etc. my knowledge isnt enough to solve this problem on my own. I tried some things, as using live data for example but that wasnt really helping me.

For further understanding:
This programm should get the data from a datalogger and display them in real time. For that, my co-worker already has a programm for windows and an API of which commands should be sent to the usb-port to get the desired answers. dataloggerViewModel.open(getusbSerialPort) opens the connection to my desired usb-device and .getFIRMWARE() sends a command to the usb-device which returns the firmware as a string (i can give the the code to these parts but as of now i dont think these have anything to do with the UI)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật