Photo by Marc Szeglat on Unsplash

🔥 Firebase-ing with Kotlin Coroutines + Flow 🌊

In this article, we’ll demonstrate using Kotlin Coroutines and 🌊 Flow with 🔥 Firebase Cloud Firestore in Android.

Shreyas Patil
Apr 30, 2020 · 4 min read

Firebase APIs are asynchronous i.e. you’ll need to register a listener if you want to read data or want the result of written data. As you might know, Kotlin coroutines are developed for asynchronous/non-blocking programming. Firebase developers have developed a separate library to use with Kotlin which is backed by Kotlin superpower! Thus, we’ll get to know how to implement firebase Cloud Firestore with Coroutines and Flow.

  • Kotlin Flow is an implementation of reactive streams made on top of coroutines and channels for Kotlin.
  • You might have used RxJava/RxKotlin. Observable and Flowable types in RxJava are an example of a structure that represents a cold stream of items. Then Kotlin Coroutines Flow 🌊 is the alternative for it.
  • Flow API in Kotlin is a better way to handle the stream of data asynchronously that executes sequentially.
  • Flow API is cold in nature ❄️ (It means it’ll only emit values whenever there is a receiver to collect it. Otherwise, the hot producer represents a host stream which emits values thought there’s no receiver. For e.g. Channels in Kotlin is a hot ♨️ stream)
  • flow{} builder is used for creating flow which can contain asynchronous and heavy operations. and value is not emitted until the terminal function collect is called.

Here’s a small demo of using the flow builder:

Demonstration of using flow. Run this code to see the output.

⚡️ Getting Started

Let’s write some code!

Open Android Studio and create a new project. Alternatively, you can simply clone this repository. This is a very simple app for demonstrating the use of Kotlin Coroutine’s Flow API to show a list of posts.

Gradle Setup

In the app module of build.gradle, include following dependencies:

Next, let’s create our model class. Create a new file and name it Post.kt.

In this application, we’ll need to manage the state of operations in our UI. For example handling the Loading, Success or Failure states. For that, we’ll create a State.kt class.

Now let’s design a Repository for this application. It’ll be a single source of the data throughout the application.🚀

This is how our repository will look like. We’ll declare these two functions here:

  • getAllPosts() — this will return a Flow<State>.
  • addPost() — this will add a post into the Cloud Firestore collection and will return Flow<State>.

Let’s implement getAllPosts()

As you can see, we are returning a flow with theflow {} builder.

  • First of all, we are emitting the Loading state which will inform the UI that our data is now in loading state.
  • Then we’re collecting posts with await() which will block this thread until it’s retrieved. Then we’re emitting the Success state along with the posts.
  • If any Exception is thrown, a catch operator will handle the exception for the upstream ⬆️ flow. Then it’ll be executed and we’ll be emitting the Failed state along with a message.

No need to write code within thetry { } catch { } block when using Flow: if any Exception is thrown on the upstream ⬆️ flow, it will be handled by the downstream catch operator.

Now we’ll implement the same for addPost() :

This should look familiar to you by now :-)

After having implemented our repository (which will handle all data reads/writes to/from Cloud Firestore, we can create a ViewModel which will be useful to interact with Android Activities. TheViewModel will be the bridge between PostsRepository and MainActivity.

Finally, it’s time to retrieve posts on the UI (MainActivity).

We’ll need to perform flow operations on the coroutine context because the flow is asynchronous and for this, we’ll need to create asuspend function to handle repository operations from ViewModel. The suspend function can be paused and resumed at a later point in time.

And the same for adding posts:

Now let’s discuss what’s happening:

  • Once we call the terminal operator collect{} on flow, this flow will be executed.
  • Whenever we’re emitting aState it will be collected here and any UI updates will be executed based on this. This is how we handled the UI state using 🌊 Flow.

This is how posts will be loaded in the application. 🚀

  • The synchronous flow of Data using aViewModel and aRepository.
  • We can handle state easily in our UI, since there’s always a defined state for each operation, such as Loading, Success, Failure.
  • We haven’t used any listener with Cloud Firestore 😄.

The following state chart outlines the getAllPosts() operation:

Statechart for getting posts from Cloud Firestore.

We have successfully implemented Cloud Firestore using Kotlin Coroutines and Flow.

The source code for this article is available in this GitHub repo.

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase…

Thanks to Peter Friese

By Firebase Developers

A summary of what has happened on the Firebase Developers publication in the past quarter. Sent once a quarter. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Shreyas Patil

Written by

📱Android Developer 💻Organizer @KotlinMumbai, 👨‍💻SWE @ScaleReal. More info:

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Shreyas Patil

Written by

📱Android Developer 💻Organizer @KotlinMumbai, 👨‍💻SWE @ScaleReal. More info:

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store