Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a MutableSharedFlow instance.
Kotlin
val eventFlow = MutableSharedFlow<[1]>() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type that does not match the event data.
Forgetting to specify the generic type.
✗ Incorrect
We create a MutableSharedFlow to broadcast events of type String.
2fill in blank
mediumComplete the code to emit an event into the SharedFlow.
Kotlin
suspend fun sendEvent(event: String) {
eventFlow.[1](event)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
collect which is for receiving events.Using
launch which is for coroutines, not flows.✗ Incorrect
Use emit to send an event to the SharedFlow.
3fill in blank
hardFix the error in collecting events from the SharedFlow.
Kotlin
suspend fun observeEvents() {
eventFlow.[1] { event ->
println("Received: $event")
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
emit which is for sending events.Using
subscribe which is not a Kotlin Flow method.✗ Incorrect
Use collect to receive events from a SharedFlow.
4fill in blank
hardFill both blanks to create a SharedFlow with replay and buffer capacity.
Kotlin
val bufferedFlow = MutableSharedFlow<String>(replay = [1], extraBufferCapacity = [2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting replay or buffer to zero when some buffering is needed.
Confusing replay and buffer parameters.
✗ Incorrect
Replay 1 event and buffer 2 extra events for smooth broadcasting.
5fill in blank
hardFill all three blanks to filter and map events in a SharedFlow.
Kotlin
val filteredFlow = eventFlow
.filter { it.length [1] [2] }
.map { it.[3]() } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Using a non-existent string method.
✗ Incorrect
Filter events longer than 3 characters and convert them to lowercase.