How to get context in Compose Desktop?
# compose-desktop
s
How to get context in Compose Desktop?
i
You mean ContextAmbient.current? On desktop there is no Context. Why is it needed?
s
I'm using Moshi to read json from assets
Yes I mean ContextAmbient.current
i
Is the project multiplatform (android, desktop, common) or desktop-only? If desktop-only then we can use something like this (to load file from "resources" folder):
Copy code
private fun loadResource(path: String): ByteArray {
    val resource = Thread.currentThread().contextClassLoader.getResource(path)
    requireNotNull(resource) { "Resource $path not found" }
    return resource.readBytes()
}
If multiplatform - we can't load resources in "common" sourceSet. But we can load them in android sourceSet with ContextAmbient.current. And in desktop sourceSet with getResource
👍 1