Abrwin21
Abrwin21

Reputation: 23

Problem calling a Composable function in an Observable

I have a problem with this especial situation. How can I call a composable function after I received a result from Observer?

@Composable
fun ProfileScreen(
    onNavigate: () -> Unit,
    account: Account,
    viewModel: DBMovieViewModel = hiltViewModel(),
    lifecycleOwner: LifecycleOwner = LocalLifecycleOwner.current
) {
    viewModel.getListMovies()
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black)
    ) {
        Column {
            ProfileComponent(account)
            viewModel.listMovies.observe(lifecycleOwner){
                MediaList(movies = it) -> Here is the problem, I can't call a Composable function in an Observable
            }
        }
    }
}

Upvotes: 2

Views: 518

Answers (1)

user3738870
user3738870

Reputation: 1622

You can use it like this:

@Composable
fun ProfileScreen(
    onNavigate: () -> Unit,
    account: Account,
    viewModel: DBMovieViewModel = hiltViewModel()
) {
    val listMovies by viewModel.listMovies.collectAsState(initial = emptyList())
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black)
    ) {
        Column {
            ProfileComponent(account)
            MediaList(movies = listMovies)
        }
    }
}

Upvotes: 3

Related Questions