Skip to content

Instantly share code, notes, and snippets.

@devrath
Created July 23, 2022 05:24
Show Gist options
  • Select an option

  • Save devrath/780c6a17eb9e65e2bb97e02a658fd592 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/780c6a17eb9e65e2bb97e02a658fd592 to your computer and use it in GitHub Desktop.
Gist for converting flow
fun getAllPosts() = flow<State<List<Post>>> {
// Emit loading state
emit(State.loading())
val snapshot = mPostsCollection.get().await()
val posts = snapshot.toObjects(Post::class.java)
// Emit success state with data
emit(State.success(posts))
}.catch {
// If exception is thrown, emit failed state along with message.
emit(State.failed(it.message.toString()))
}.flowOn(Dispatchers.IO)
// Reference: https://medium.com/firebase-developers/firebase-ing-with-kotlin-coroutines-flow-dab1bc364816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment