Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a Flow that observes all users from the DAO.
Android Kotlin
fun getAllUsers(): [1]<List<User>> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using LiveData instead of Flow when Flow is required.
Returning a plain List instead of a reactive stream.
✗ Incorrect
Room supports Flow to provide reactive streams of data.
2fill in blank
mediumComplete the code to collect data from the Flow in a coroutine scope.
Android Kotlin
lifecycleScope.launch {
userDao.getAllUsers().[1] { users ->
// update UI with users
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using observe which is for LiveData, not Flow.
Trying to subscribe like RxJava instead of collect.
✗ Incorrect
To get data from a Flow, you collect it inside a coroutine.
3fill in blank
hardFix the error in the DAO function to return a Flow of users.
Android Kotlin
@Query("SELECT * FROM users") fun getUsers(): [1]<List<User>>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning List directly which is not reactive.
Using LiveData instead of Flow in Kotlin coroutines.
✗ Incorrect
The DAO must return a Flow to enable reactive updates.
4fill in blank
hardFill both blanks to create a Flow that filters users older than 18.
Android Kotlin
fun getAdultUsers(): [1]<List<User>> = userDao.getAllUsers() .[2] { list -> list.filter { it.age > 18 } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter on Flow which filters emissions, not list items.
Returning LiveData instead of Flow.
✗ Incorrect
Use Flow as return type and map to transform the emitted list.
5fill in blank
hardFill the blanks to debounce rapid Flow emissions and collect them.
Android Kotlin
lifecycleScope.launch {
userDao.getAllUsers()
.[1](300)
.[2] { users ->
// update UI
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect without debounce causing too many updates.
Using map instead of debounce for timing control.
✗ Incorrect
Use debounce to wait for 300ms of silence, collectLatest to cancel previous work.