https://kotlinlang.org logo
s

samueldple

06/24/2020, 12:21 PM
When using a scaffold with a conditional FAB, I find I have to cast to compile:
Copy code
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?
😅 2
d

Dominaezzz

06/24/2020, 1:46 PM
What is the declaration of
floatingActionButton
?
s

samueldple

06/24/2020, 1:58 PM
Copy code
floatingActionButton: @Composable (() -> Unit)? = null,
d

Dominaezzz

06/24/2020, 2:40 PM
Shouldn't that be
floatingActionButton: (@Composable () -> Unit)? = null
?
m

matvei

06/24/2020, 2:51 PM
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
Copy code
floatingActionButton = {
    if (condition) { 
        FloatingActionButton(onClick = {backStack.push(Routing.EditDrink(null))}) {
            Icon(Icons.Default.Add)
        }
}
and it should work just fine
👍 1
s

samueldple

06/24/2020, 2:57 PM
That worked perfectly, thanks!
d

Dominaezzz

06/24/2020, 3:00 PM
Do the parenthesis not matter?
s

samueldple

06/24/2020, 6:16 PM
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
5 Views