Kotlin Flow in Android with Example
Kotlin Flow is one of the latest addition to the Kotlin Coroutines. With Kotlin Flow we can handle streams of data asynchronously which is being executed sequentially.
What are we going to build in this article?
We will build a simple app that fetches some data from API and shows it on the screen. It’s a simple app demonstrating the Kotlin flow working. It will use MVVM architecture.
- Good knowledge of Android
- Knowledge of Kotlin
- Basics of MVVM Architecture
- Basics of Retrofit Library
- Basics of Kotlin coroutines
- Basics of View Binding
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Project Structure
We will be following some patterns to keep our files. Create folders and files according to this project structure. Uses will be explained later in this article.
Step 3: Add required dependencies
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
// Retrofit dependency
// json convertor factory
// Coroutines(includes kotlin flow)
// lifecycle components
Step 4: Working with the activity_main.xml
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
It UI looks like this:
Step 5: Working with the API
We will be using https://jsonplaceholder.typicode.com/comments API, It gives some JSON data when id is passed as a path. For example, https://jsonplaceholder.typicode.com/comments/2 gives JSON which contains some random data. We will be using this data and show it on the screen using kotlin flow. Open model > CommentModel and create a model class to parse data that is received from the API.
We need to create a dataclass for this response. Add the following code in CommentModel
Creating API Interface
We need to create an API interface to call the API using a retrofit. Open network > ApiService and add the following code
Let’s add some helper classes to handle the loading or error state of API. Open network > CommentApiState. Refer to the comment in the code for an explanation.
Open utils > AppConfig and add code to create API service, which will be used to make API calls.
The API part of the App is complete. Now we need to work on ViewModel and repository.
Step 6: Working with the Repository
Open repository > CommentsRepository. Add the following code. Refer to the comments for an explanation.
Step 7: Working with the ViewModel
Open ViewModel > CommentViewModel.Add the following code. Refer to the comments for an explanation.
we are almost done, we now need to call the API from view(MainActivity) and show the data on the screen.
Step 8: Working with the view (MainActivity.kt)
Open presentation > MainActivity.kt. Add the following code, refer to the comments for explanation.
Now run the app, put some numbers and click search. Enter any number from 1-500, It will return a successful state.
Get the complete project from GitHub.