How to call Kotlin coroutine in composable function callbacks?

Rawieo

I want to call a suspend-function inside of a callback of composable-function.

suspend fun getLocation(): Location? { /* ... */ }

@Composable
fun F() {

    val (location, setLocation) = remember { mutableStateOf<Location?>(null) }

    val getLocationOnClick: () -> Unit = {
        /* setLocation __MAGIC__ getLocation */
    }

    Button(onClick = getLocationOnClick) {
        Text("detectLocation")
    }

}

If I would have used Rx then I could just subscribe.

I could do invokeOnCompletion and then getCompleted, but that API is experimental.

I can't use launchInComposition in getLocationOnClick because launchInComposition is @Composable and getLocationOnClick can not be @Composable.

What would be the best way to get result of a suspending function inside a regular function, inside @Composable function?

2jan222

You can use the viewModelScope of a ViewModel or any other coroutine scope.

Example of Deleting Action for an Item from LazyColumnFor which requires a suspend call handled by a ViewModel.

     class ItemsViewModel : ViewModel() {

        private val _itemList = MutableLiveData<List<Any>>()
        val itemList: LiveData<List<Any>>
            get() = _itemList

        fun deleteItem(item: Any) {
            viewModelScope.launch(Dispatchers.IO) {
                TODO("Fill Coroutine Scope with your suspend call")       
            }
        }
    }

    @Composable
    fun Example() {
        val itemsVM: ItemsViewModel = viewModel()
        val list: State<List<Any>?> = itemsVM.itemList.observeAsState()
        list.value.let { it: List<Any>? ->
            if (it != null) {
                LazyColumnFor(items = it) { item: Any ->
                    ListItem(
                        item = item,
                        onDeleteSelf = {
                            itemsVM.deleteItem(item)
                        }
                    )
                }
            } // else EmptyDialog()
        }
    }

    @Composable
    private fun ListItem(item: Any, onDeleteSelf: () -> Unit) {
        Row {
            Text(item.toString())
            IconButton(
                onClick = onDeleteSelf,
                icon = { Icons.Filled.Delete }
            )
        }
    }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

how to use Coroutine in kotlin to call a function every second

分類Dev

how create a coroutine inside a Controller method in order to call a suspend function

分類Dev

How to call a static JNI function from Kotlin?

分類Dev

How can I catch an exception in Kotlin coroutine when I am awaiting it in another function?

分類Dev

How to rate limit a coroutine and re-call the coroutine after the limit?

分類Dev

Execute coroutine from `call_soon` callback function

分類Dev

@composable invocations can only happen from the context of an @composable function

分類Dev

@composable invocations can only happen from the context of an @composable function

分類Dev

Kotlin: how to make a function call using the first argument's default value and passing a value for the second?

分類Dev

How to use Ramda Pipe function with a mix of promises and static callbacks?

分類Dev

How to create a Generic Function in Kotlin

分類Dev

How to create a callback function in Kotlin?

分類Dev

How to properly exec() a coroutine

分類Dev

Is there a way to call an anonymous function from within itself in Kotlin?

分類Dev

Kotlin alternative to Python's coroutine yield and send

分類Dev

Kotlin coroutine list returning null value

分類Dev

Returning from inner nested coroutine by label Kotlin

分類Dev

How to call a Swift function in Rust?

分類Dev

how to call function with parameter in JSX?

分類Dev

How to Call a Function with a Parameter in Blazor?

分類Dev

(JQuery Plugin) How to call function?

分類Dev

kotlin and android: is it necessary to cancel previously setup callbacks?

分類Dev

How to expect one function to call another function?

分類Dev

How to expect one function to call another function?

分類Dev

How to expect one function to call another function?

分類Dev

How to correctly handle a call function in a function with javascript

分類Dev

How to create a function that delays call to given function?

分類Dev

how to call a function from another function in Jquery

分類Dev

Android: Coroutine doesn't always work on call

Related 関連記事

  1. 1

    how to use Coroutine in kotlin to call a function every second

  2. 2

    how create a coroutine inside a Controller method in order to call a suspend function

  3. 3

    How to call a static JNI function from Kotlin?

  4. 4

    How can I catch an exception in Kotlin coroutine when I am awaiting it in another function?

  5. 5

    How to rate limit a coroutine and re-call the coroutine after the limit?

  6. 6

    Execute coroutine from `call_soon` callback function

  7. 7

    @composable invocations can only happen from the context of an @composable function

  8. 8

    @composable invocations can only happen from the context of an @composable function

  9. 9

    Kotlin: how to make a function call using the first argument's default value and passing a value for the second?

  10. 10

    How to use Ramda Pipe function with a mix of promises and static callbacks?

  11. 11

    How to create a Generic Function in Kotlin

  12. 12

    How to create a callback function in Kotlin?

  13. 13

    How to properly exec() a coroutine

  14. 14

    Is there a way to call an anonymous function from within itself in Kotlin?

  15. 15

    Kotlin alternative to Python's coroutine yield and send

  16. 16

    Kotlin coroutine list returning null value

  17. 17

    Returning from inner nested coroutine by label Kotlin

  18. 18

    How to call a Swift function in Rust?

  19. 19

    how to call function with parameter in JSX?

  20. 20

    How to Call a Function with a Parameter in Blazor?

  21. 21

    (JQuery Plugin) How to call function?

  22. 22

    kotlin and android: is it necessary to cancel previously setup callbacks?

  23. 23

    How to expect one function to call another function?

  24. 24

    How to expect one function to call another function?

  25. 25

    How to expect one function to call another function?

  26. 26

    How to correctly handle a call function in a function with javascript

  27. 27

    How to create a function that delays call to given function?

  28. 28

    how to call a function from another function in Jquery

  29. 29

    Android: Coroutine doesn't always work on call

ホットタグ

アーカイブ