coroutine Flow : Not sure how to convert a Cursor to this method's return type
Do not use both suspend and Flow<> on the same method! Like this
@Query("SELECT * FROM user") suspend fun loadAll(): Flow<Array<User>>
Just
@Query("SELECT * FROM user") suspend fun loadAll(): Array<User>
OR
@Query("SELECT * FROM user") fun loadAll(): Flow<Array<User>>
You've left a comment that you're using 2.1.0
as Room version. Please give this a read and you'll find that 2.2.0-alpha2
is required for using Flow
. Update the version and it should work.
Room 2.2.0-alpha02 advertised Flow support
I tried the approaches provided in the other answers, namely avoiding Flow
and LiveData
in DAO
that have suspend
function but I was still getting the error.
In the end, my problem was related to my dependencies. I had multiple room dependencies like:
def room_version = "2.2.5" implementation "androidx.room:room-runtime:$room_version" kapt "android.arch.persistence.room:compiler:1.1.1" implementation "androidx.room:room-ktx:$room_version"
When only the last one was required (SEE EDIT):
implementation "androidx.room:room-ktx:2.2.5"
EDIT
Actually, this is not event true. It compiled fine but when I launched the app, I had an error at runtime.
I could make it work by swapping the compiler version:
def room_version = "2.2.5" implementation "androidx.room:room-runtime:$room_version" kapt "androidx.room:room-compiler:$room_version" implementation "androidx.room:room-ktx:$room_version"