3

From a project generated con Kotlin Multiplatform Wizard

I'm testing a web application with wasmJs ([email protected])

If I try to display a value from an array string loaded from string resources, I get a WebAssembly Exception

my code:

Text(text = stringArrayResource(Res.array.item_list)[0])

my string resource file:

<resources>
    <string name="app_name">TestWeb</string>

    <string-array name="item_list">
        <item>item 1</item>
        <item>item 2</item>
        <item>item 3</item>
    </string-array>

</resources>

the error in the web (tried it in chrome, and firefox): enter image description here

additional: it show [item 1, item 2, item 3] If I do:

Text(text = stringArrayResource(Res.array.item_list).toString())

Likewise in android app, and desktop app the correct value is displayed. The problem is only in web...

1 Answer 1

0

Just got the same error to access a string-array on WasmJs ; fixed it by putting a runCatching around the call :

  • first drawing pass => fail (not visible on screen, but visible in logs)

  • second drawing pass => working fine 🤷

Seems like the ressources are not already loaded while drawing the first time, so it crashes and doesn't try the second pass.

@Composable
fun Month.prettyShortName(): String {
    return runCatching {
        val array = stringArrayResource(Res.array.months)
        val month = array[this.number - 1]
        return@runCatching if (month.length > 4) month.take(3).plus(".") else month.take(4)
    }
        .onFailure {
            println("Failed to get Month name :${it.stackTraceToString()}")
        }
        .getOrDefault("Fail")
}

@Composable
fun DayOfWeek.prettyShortName(): String {
    return runCatching {
        val array = stringArrayResource(Res.array.days_of_week)
        val day = array[this.ordinal]
        return@runCatching day.take(3)
    }
        .onFailure {
            println("Failed to get DayOfWeek name :${it.stackTraceToString()}")
        }
        .getOrDefault("Fail")
}

StackTrace


Failed to get Month name :IndexOutOfBoundsException: Empty list doesn't contain element at index 5.
    at kotlin.captureStackTrace (webpack-internal:///./kotlin/kaizenMain.uninstantiated.mjs:21:44)
    at <XXX:main>.kotlin.captureStackTrace__externalAdapter (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13235]:0x3edcbb)
    at <XXX:main>.kotlin.Throwable.<init> (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13225]:0x3edb11)
    at <XXX:main>.kotlin.Throwable.<init> (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13226]:0x3edb4a)
    at <XXX:main>.kotlin.Exception.<init> (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13605]:0x3f217d)
    at <XXX:main>.kotlin.RuntimeException.<init> (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13595]:0x3f1f9f)
    at <XXX:main>.kotlin.IndexOutOfBoundsException.<init> (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[13587]:0x3f1dfa)
    at <XXX:main>.kotlin.collections.EmptyList.get (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[10610]:0x3c94ed)
    at <XXX:main>.kotlin.collections.EmptyList.get (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[10611]:0x3c94f9)
    at <XXX:main>.XXX.presentation.prettyShortName (http://localhost:8080/fb1bf6fdb5d16ac6ba41.wasm:wasm-function[89983]:0x952969)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.