简体   繁体   中英

getting error of suspend function while using coroutine

also, I'm calling my suspend function inside a coroutine scope, I'm getting an error that Suspends functions can only be called within a coroutine body. how's that happening?


  lifecycleScope.launchWhenCreated{
                viewModel.tickets.observe(viewLifecycleOwner,  {
                    it?.let { data ->
                        adapter.submitData(data)
                    }
                })
        }

Hi you are calling suspend function inside observe lambda.

Change the order like this:

   viewModel.tickets.observe(viewLifecycleOwner,  { data ->
           lifecycleScope.launchWhenCreated{
                data?.let { list ->
                    adapter.submitData(list)
                }
            })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM