Why is it when I try to use `MaterialTheme.colors....
# coroutines
c
Why is it when I try to use
MaterialTheme.colors.*
I get an error of "@Composable invocations can only happen from the context of...", but if I just use a string there instead then it works fine, which shows that my @Composable is actually setup correctly. Code snipipets in thread.
Copy code
LazyColumn(content = {
    items(
        listOf(
            "test1",
            "test2",
        )
    ) {
        Text(text = it.toString())
    }
})
This works
Copy code
LazyColumn(content = {
    items(
        listOf(
            MaterialTheme.colors.primary,
            MaterialTheme.colors.onSecondary,
        )
    ) {
        Text(text = it.toString())
    }
})
This does not work.
r
I don't have the code in front of me, I'm guessing some part
MaterialTheme
of is
@Composable
(because of the ambient). Note that `LazyColumn`'s
content
isn't composable, but the `items`'s content is.
☝️ 1
c
So what's my solution? I can't use MaterialTheme.colors.* as part of my input data?
a
/me gazes at the current channel 🤔
☝️ 3
c
Whoops!
a
Anyway it's because the LazyColumn content lambda isn't itself a composable function
But the body of the items blocks are
c
I just want to reference these colors... 😭
a
Make the list outside of the actual LazyColumn call and reference the captured val inside
c
Hm. I just pulled it out to a top level var in the file
Copy code
val list = listOf(
    MaterialTheme.colors.primary,
    MaterialTheme.colors.onSecondary,
)
and it has the same error.
a
yes, it needs to be in a
@Composable
function.
Copy code
val list = listOf(
    MaterialTheme.colors.primary,
    MaterialTheme.colors.onSecondary,
)
LazyColumn {
  items(list) {
    // ...
  }
}
c
It worked. And I dont wanna lie and say im not defeated. lol I feel so stupid. oh well. Thanks @Adam Powell
😅 2