Creating Flow Using Flow Builder in Kotlin

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Creating Flow Using Flow Builder in Kotlin

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

In this blog, we are going to discuss the different types of flow builders and how to create Flow using Flow Builder. We will learn to create Flow with examples.

This blog is a part of the series I have written on Flow API in Kotlin:

Let's get started.

We are going to cover the following:

  • Types of flow builders
  • Creating Flow Using Flow Builder

Types of flow builders

There are 4 types of flow builders:

  1. flowOf(): It is used to create flow from a given set of items.
  2. asFlow(): It is an extension function that helps to convert type into flows.
  3. flow{}: This is what we have used in the Hello World example of Flow.
  4. channelFlow{}: This builder creates flow with the elements using send provided by the builder itself.

Examples:

flowOf()

flowOf(4, 2, 5, 1, 7)
.collect {
    Log.d(TAG, it.toString())
}

asFlow()

(1..5).asFlow()
.collect {
    Log.d(TAG, it.toString())
}

flow{}

flow {
    (0..10).forEach {
        emit(it)
    }
}
.collect {
    Log.d(TAG, it.toString())
}

channelFlow{}

channelFlow {
    (0..10).forEach {
        send(it)
    }
}
.collect {
    Log.d(TAG, it.toString())
}

Now, we will learn how to create our Flow using the Flow builder. We can create our Flow for any task using the Flow Builder.

Creating Flow Using Flow Builder

Let's learn it through examples.

1. Move File from one location to another location

Here, we will create our Flow using the Flow Builder for moving the file from one location to another in the background thread and send the completion status on Main Thread.

val moveFileflow = flow {
        // move file on background thread
        FileUtils.move(source, destination)
        emit("Done")
}
.flowOn(Dispatchers.IO)
CoroutineScope(Dispatchers.Main).launch {
    moveFileflow.collect {
        // when it is done
    }
}

2. Downloading an Image

Here, we will create our Flow using the Flow Builder for downloading the Image which will download the Image in the background thread and keep sending the progress to the collector on the Main thread.

val downloadImageflow = flow {
        // start downloading
        // send progress
        emit(10)
        // downloading...
        // ......
        // send progress
        emit(75)
        // downloading...
        // ......
        // send progress
        emit(100)
}
.flowOn(Dispatchers.IO)
CoroutineScope(Dispatchers.Main).launch {
    downloadImageflow.collect {
        // we will get the progress here
    }
}

This is how we can create our Flow.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.