https://kotlinlang.org logo
Title
d

Dirk Hoffmann

02/10/2021, 4:56 PM
as well as
IconButton(onClick = scaffoldStateClick())
gives me:
The feature "unit conversion" is disabled
am a bit clueless on how to fix both...
j

jim

02/10/2021, 5:30 PM
I can't see your definition of
scaffoldStateClick()
, but two questions: (1) the
onClick
parameter expects a lambda/function, but you are INVOKING
scaffoldStateClick()
, not passing it as a parameter. Did you mean
::scaffoldStateClick
or
scaffoldStateClick
? (2) If you did intend to invoke
scaffoldStateClick()
and it returns a lambda, are you sure the returned lambda is of type
()->Unit
?
d

Dirk Hoffmann

02/10/2021, 5:39 PM
fun scaffoldStateClick() = {
    val adScaffoldState = ADState.ADScaffoldState
    if (adScaffoldState.drawerState().isClosed) {
        adScaffoldState.drawerState().open()
    } else {
        adScaffoldState.drawerState().close()
    }
}
ADState.ADScaffoldState
is a singleton Object that keeps all my states and provides methods on them (don't know if that is good design ... is it??)
j

jim

02/10/2021, 5:46 PM
You should pass your
ADScaffoldState
instance as a parameter, not use a singleton object. You always want to develop your widgets as self-contained reusable chunks that are pure functional transforms of their parameters. Relying on a global singleton violates several of those principles. Also, your
scaffoldStateClick()
does not return a unit lambda, so that's why you're getting a type error. You probably want your code to be more like
IconButton(onClick = { scaffoldStateClick(adScaffoldState) })
👍 1