Exercise 5.01 – Using Kotlin Flow in an Android app
For this exercise, you will be continuing the movie app you worked on in Exercise 4.01 – Adding tests to coroutines in an Android app. This application displays the movies that are currently playing in cinemas. You will be adding Kotlin Flow to the project by following these steps:
- Open the movie app you worked on in Exercise 4.01 – Adding tests to coroutines in an Android app in Android Studio.
- Go to the
MovieRepository
class and add a newfetchMoviesFlow()
function that uses aflow
builder to return a Flow and emits the list of movies fromMovieService
, as shown in the following snippet:fun fetchMoviesFlow(): Flow<List<Movie>> { return flow { emit(movieService.getMovies(apiKey).results) }.flowOn(Dispatchers.IO) }
This is the same as the fetchMovies()
function, but this function uses...