[英]How to use await function in coroutine from non-suspended function in Kotlin Android?
[英]Kotlin coroutine - Type mismatch when trying to return ArrayList from a suspended function
我有一個掛起的方法,它應該返回一個 ArrayList。 但是,在嘗試這樣做時,IDE 會在協程范圍內突出顯示類型不匹配錯誤。
我的方法——
private suspend fun dbsearchHelper(query: String): ArrayList<String> {
return withContext(Dispatchers.Default) {
if (query.length > 3) {
return@withContext SupportedPackagesManager.findStoreByName(query)
}
}
}
錯誤 -
Type interference failed. Expected type mismatch:
required: kotlin.collections.ArrayList<String>
found: Any!
findStoreByName
是一個 Java 方法。
我會做錯什么?
你需要有一個 else 分支,這樣 Kotlin 才能明確地知道你返回了什么。
在您的情況下,Kotlin 將 withContext 的返回類型設置為 Any,因為如果 (query.length <= 3),您的 withContext 返回 Unit。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.