Why is this complaining about a miss-type? map supposed to transfor the value of an observable to another, but it's expecting me to return an observable from the map
override fun fetchExercises(): Observable<List<Exercise>> {
    return FirebaseDatabase.getInstance()
        .getReference("exercises")
        .observeSingleEvent()
        .map { snapshot: DataSnapshot? -> object: List<Exercise>
            // Error here
            return listOf<Exercise>()
        }
    }
fun DatabaseReference.observeSingleEvent(): Observable<DataSnapshot> {
    return Observable.create(object : Observable.OnSubscribe<DataSnapshot> {
        override fun call(subscriber: Subscriber<in DataSnapshot>) {
            val valueEventListener = object: ValueEventListener {
                override fun onDataChange(snapshot: DataSnapshot?) {
                    subscriber.onNext(snapshot)
                    subscriber.onCompleted()
                }
                override fun onCancelled(error: DatabaseError?) {
                    subscriber.onError(FirebaseDatabaseThrowable(error))
                }
            }
            addListenerForSingleValueEvent(valueEventListener)
        }
    })
}
                @zsmb13 has pointed out the correct answer right there. Here, I would like to comment on some mistake made in your lambda expression.
.map { snapshot: DataSnapshot? -> object: List<Exercise>
        // Error here
        return listOf<Exercise>()
}
object: List<Exercise> here is not a valid syntax since the body goes after an -> sign inside the lambda expression. We do not specify the return type inside the body.
Also, the return here means to return from fetchExercises() as described in Kotlin docs:
A return statement without a label always returns from the function declared with the fun keyword. This means that a return inside a lambda expression will return from the enclosing function, whereas a return inside an anonymous function will return from the anonymous function itself.
As what @zsmb13 said, return is not needed in most of the case. If you really want it(for flow control), you can use qualified returns:
//This is answer from @zsmb13
.map { snapshot: DataSnapshot? ->
    return@map listOf<Exercise>()
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With