Converting Firebase API Callbacks to Kotlin Flows

Using Kotlin’s callbackFlow builder on Android

Image for post
Image for post
Photo by Michal Matlon on Unsplash

Button.clickFlow()

buttonDownload.onClick { viewModel.download() }
buttonDownload.setOnClickListener { viewModel.download() }

Querying a Record from Firestore

CollectionReference.get()
.addOnCompleteListener { snapshot ->
// Do something with snapshot
}
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:0"
import kotlinx.coroutines.tasks.awaitval snapshot = CollectionReference.get().await()
implementation “androidx.lifecycle:lifecycle-runtime-ktx:0”
GlobalScope.launch {  } // Naughty, naughty!

Watching for changes to a Firestore Database

Downloading a File from Firebase Storage

Bonus Material

Written by

Freelance Android Dev. Google Developer Expert. Full Time Remote. Part Time Buzzword Hacker.

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