https://kotlinlang.org logo
#compose
Title
# compose
b

Bryan Lee

12/17/2019, 11:08 PM
Can I use
Copy code
val context = +ambient(ContextAmbient)
from a composable nested within setViewContent{ }? Currently seem to be getting an NPE because of this. Is there an alternative way of getting context? EDIT: Currently I'm doing this from my activity to get context to a listener into my composable function.
Copy code
val context: Context = this
setViewContent {
        MapApp(context = context)
}
l

Leland Richardson [G]

12/17/2019, 11:53 PM
what code is giving you the NPE?
ohhh
i gotcha
we should fix this. can you file a bug?
in the meantime, you can do something like this….
Copy code
fun Activity.mySetViewContent(content: @Composable () -> Unit) = setViewContent { ContextAmbient.Provider(this, content) }
but you shouldn’t have to do that… we just haven’t provided all of the ambients we should have been in the
setViewContent
utility function
👍🏻 1
b

Bryan Lee

12/17/2019, 11:58 PM
sure. and gotcha, i'll do that for now. thanks!
11 Views