Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
Type mismatch in lambda function - Kotlin

I want the text to be taken from string resource, depends on item.route, which is specified before bellow code.

But whole "when" expression gives me the error: "Type mismatch: inferred type is () -> Int but Int was expected." I don't have idea how to resolve it.


Text(
        text = stringResource(id = {
            when (item.route) {
                "menu_screen" -> R.string.menu
                "orders_screen" -> R.string.orders
                else -> R.string.orders
            }
        }),
        textAlign = TextAlign.Center
    )
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Just remove the extra curly brace ;)

Text(
    text = stringResource(id = 
        when (item.route) {
            "menu_screen" -> R.string.menu
            "orders_screen" -> R.string.orders
            else -> R.string.orders
        }
    ),
    textAlign = TextAlign.Center
)
over 4 years ago · Santiago Trujillo Relatório

0

When you put bare brackets { } around something, it is interpreted as a lambda function. By "bare" I mean not after a class/fun/when/if/else, etc. You need to remove the extra brackets you put in there.

Text(
    text = stringResource(id = 
        when (item.route) {
            "menu_screen" -> R.string.menu
            "orders_screen" -> R.string.orders
            else -> R.string.orders
        }
    ),
    textAlign = TextAlign.Center
)

Some other languages (such as Java) let you surround code with brackets to limit the scope of variables declared inside or to compute something that takes multiple expressions. To do this in Kotlin, you have to use a scope inline function such as run { }.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda