Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda