Hi i can assign a composable function to a `var` o...
# compose
t
Hi i can assign a composable function to a
var
or
val
as follows:-
val functionVariable: @Composable () -> Unit = { myComposableFunction() }
however is it possible to define a "getter" NON COMPOSABLE function that returns this variable whatever i try Android Studio is highlighting compile time errors saying the getters have to be annotated @Composable if i am simply returning a value thats composable why is that?
m
a mutable var has get and set methods. Both need to be annotated, otherwise it wouldn’t respect the signature return type. What is the question really, can you re-formulate ?
a
You can definitely do this:
Copy code
val functionVariable: @Composable () -> Unit
    get() = { myComposableFunction() }
🙌 1
t
for example i employ compose navigation and set the topBar title as follows:
override fun getTitleByRoute(route: String): String = when (route) {
TopicAlertDestination.route -> TopicAlertDestination.toolBarSubTitle
HistoryDestination.route -> HistoryDestination.toolBarSubTitle
StackGridDestination.route -> StackGridDestination.toolBarSubTitle
BookmarkDestination.route -> BookmarkDestination.toolBarSubTitle
else -> _TODO_("Unknown route = $route")
}
which is called as follows
appBarSubtitle = myNavigation.getTitleByRoute(backStackEntry.destination.route!!)
i wish to have a similar function that returns a composable based on the
backStackEntry.destination.route
override fun getComposableByRoute(route: String): @Composable () -> Unit = when (route) {
TopicAlertDestination.route -> functionVariable1
HistoryDestination.route -> functionVariable2
StackGridDestination.route -> functionVariable3
BookmarkDestination.route -> functionVariable4
else -> _TODO_("Unknown route = $route")
}