s

    samueldple

    2 years ago
    When using a scaffold with a conditional FAB, I find I have to cast to compile:
    floatingActionButton = if (condition) { 
        @Composable {
            FloatingActionButton(onClick = {backStack.push(Routing.EditDrink(null))}) {
                Icon(Icons.Default.Add)
            }
        } as @Composable() () -> Unit
    } else null
    Or I get an "Expected type was Nothing?" error, but when casting the cast shows as a useless cast. Am I doing something stupid?
    Dominaezzz

    Dominaezzz

    2 years ago
    What is the declaration of
    floatingActionButton
    ?
    s

    samueldple

    2 years ago
    floatingActionButton: @Composable (() -> Unit)? = null,
    Dominaezzz

    Dominaezzz

    2 years ago
    Shouldn't that be
    floatingActionButton: (@Composable () -> Unit)? = null
    ?
    matvei

    matvei

    2 years ago
    Hello! I don't think parenthesis matter, I will try it, thanks. In the mean time, do you really need to return null? you can do
    floatingActionButton = {
        if (condition) { 
            FloatingActionButton(onClick = {backStack.push(Routing.EditDrink(null))}) {
                Icon(Icons.Default.Add)
            }
    }
    and it should work just fine
    s

    samueldple

    2 years ago
    That worked perfectly, thanks!
    Dominaezzz

    Dominaezzz

    2 years ago
    Do the parenthesis not matter?
    s

    samueldple

    2 years ago
    I don't think so - they're just there for grouping for the nullibility. Other than that it's just
    @Composable (() -> Unit)
    vs
    @Composable () -> Unit
    , and in both cases the
    @Composable
    applies to the whole function type