lifecycleScope.launch Unresolved Reference

  Kiến thức lập trình

I have the import of lifecycleScope and its pink like its being used and its working but
the lifecycle Scope is red saying unresolved Reference

I was trying to call an Api and with the User in response update the viewmodel to have the data saved there
and the idea is to go to login screen if it fails or the token isnt saved or go to the HomeScreen if all worked

if (token.isNotEmpty()) {
    val api = MiAPI.instance
    val userViewModel = UserViewModel()

    viewLifecycleOwner.lifecycleScope.launch {
        try {
            val user = api.getUser(token)
            userViewModel.setUser(user)
            navController.navigate(route = AppScreens.CategoriesScreen.route)
        } catch (e: Exception) {
            navController.navigate(route = AppScreens.LoginScreen.route)
        }
    }
} else {
    navController.navigate(route = AppScreens.LoginScreen.route)
}

New contributor

Ángel David Gutierrez Orellana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT