mino24
mino24

Reputation: 5

Jetpack compose TopAppBar gives a call error

I get an error when I write the following in Jetpack compose TopAppBar. I think it's a rudimentary mistake, but I don't know. write error

TopAppBar( // ← error:None of the following functions can be called with the arguments supplied.
    title = Text(text = "hogrhoge"),
    actions = {IconButton(...)}

Upvotes: 0

Views: 1334

Answers (2)

Ritt
Ritt

Reputation: 3349

If you check the Scaffold function definition

fun Scaffold(
  modifier: Modifier = Modifier,
  toastHostState: ToastHostState = remember { ToastHostState() },
  toastHost: @Composable (ToastHostState) -> Unit = { ToastHost(it) },
  statusBarMode: StatusBarMode = StatusBarMode.Red,
  topBar: @Composable () -> Unit = {}, // it's expecting a lambda
  bottomBar: @Composable () -> Unit = {},
  floatingActionButton: @Composable () -> Unit = {},
  backgroundColor: Color = NicolletTheme.colors2.backgroundPrimary,
  contentColor: Color = NicolletTheme.colors2.textPrimary,
  content: @Composable (PaddingValues) -> Unit
)

It's expecting a lambda, you need to wrap it inside a {}

TopAppBar(
  title  = { Text(title = "aa") //your composable } 
)

Upvotes: 1

Jan Bína
Jan Bína

Reputation: 7218

Parameter title is of type @Composable () -> Unit, which means you have to pass composable function there. You are instead passing value returned from Text function, which is Unit. And it's the same with actions parameter. What you should do is this:

TopAppBar(
    title = { Text(...) },
    actions = { IconButton(...) },
    ...
)

Upvotes: 0

Related Questions