I’m currently learning KMP and Compose Multiplatform. I’m using voyager as navigation and substitute to viewmodel.
Here’s the code for Screen Model, just simple data retrieving. Already test this part standalone and didn’t cause any issue.
class MainScreenModel : ScreenModel {
private val _manga: MutableStateFlow<List<MangaDto>> = MutableStateFlow(emptyList())
val manga = _manga.asStateFlow()
init {
screenModelScope.launch {
_manga.update {
Greeting().getPopularManga()
}
}
}
}
And here is the screen
internal class MainScreen : Screen {
@Composable
override fun Content() {
val screenModel = rememberScreenModel { MainScreenModel() }
val searchQuery by screenModel.searchQuery.collectAsState()
val manga by screenModel.manga.collectAsState()
Column(
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.padding(8.dp)
) {
TextField(
value = searchQuery,
onValueChange = { _newSearchQuery: String ->
screenModel.onEvent(
MainScreenModel.MainScreenEvent.OnSearchQueryChanged(
_newSearchQuery
)
)
},
shape = RoundedCornerShape(8.dp),
leadingIcon = {
Icon(
imageVector = Icons.Default.Search,
contentDescription = null
)
},
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
),
placeholder = {
Text(
text = "Search",
fontWeight = FontWeight.Bold,
)
},
modifier = Modifier.width(700.dp)
)
LazyVerticalGrid(
columns = GridCells.FixedSize(150.dp),
contentPadding = PaddingValues(4.dp),
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalArrangement = Arrangement.spacedBy(6.dp)
) {
items(manga) { manga_ ->
MangaCard(
id = manga_.id,
title = manga_.attributes.title.en
?: manga_.attributes.title.romanized.orEmpty(),
imageUrl = "https://uploads.mangadex.org/covers/${manga_.id}/${manga_.relationships.find { it.type == "cover_art" }?.attributes?.fileName.orEmpty()}",
onClicked = {
}
)
}
}
}
}
}
The problem only occur in desktop platform with message:
[CoroutineName(ui.MainScreen:ui.MainScreenModel:default:ScreenModelCoroutineScope), StandaloneCoroutine{Cancelled}@66207bc1, Dispatchers.Main[missing]]
I already added the coroutine core into commonMain dependencies block. Also i make sure if the version of coroutine android on android specific is the same with the core.