How to preview a composable which haves a paramete...
# compose
p
How to preview a composable which haves a parameter that needs a lambda with two parameters?
Copy code
fun FlightsForAirport(onFavoriteSelected: (String, String) -> Unit, modifier: Modifier = Modifier)
I try calling that composable with this:
Copy code
FlightsForAirport({})
But it gives me this error:
Expected 2 parameters of types String, String
s
{ _, _ -> }
p
omg, how it's suppossed I must disscover that? 😮
it worked perfectly! thanks
s
You could write normal Kotlin and name the two parameters coming in, just like you would with any normal lambda, this has nothing to do with compose. Then the IDE would help you turning them to underscores if you weren't using the variable anyway
p
well it was the first thing I tryed and didn't worked
Copy code
{"",""}
that throws error too
s
That's not how you add parameter names to a lambda
p
maybe can you show me how you do it? after 20 mins searching in google "how to add parameters to lambda function in kotlin" can't find anything that works in this situation
f
I just googled "Kotlin lambda" and the first link is official documentation: https://kotlinlang.org/docs/lambdas.html#higher-order-functions The second code example uses
acc: Int, i: Int ->
syntax
p
I tryed with
Copy code
FlightsForAirport({"": String, "": String ->})
and didn't work too. Maybe can you test it and tell me how you wrote it to make it work?
s
""
is not a valid variable name. You can't do
val "" = 5
You gotta choose a proper variable name.
Also the comment right above yours by Filip gives a working example you can copy paste:
The second code example uses
acc: Int, i: Int ->
syntax
f
Or just read the documentation and don't copy code if you are not sure you understand how it works
âž• 2
p
I read the documentation, but I don't understand it, it's too much for me at my current level
I need to try the code and to ask you guys to recover from my errors and step up
finally it working with this:
Copy code
FlightsForAirport({acc: String, i: String -> })
i really don't understand how can that work, it is not easily understandable to be forced to put the name of the variable instead of a variable if you are testing the function, it is a very rare thing to do
even now I don't understand why you must put that to test it, should be something more logic like "","" instead of {acc: String, i: String -> }
s
What is "","" ?
p
{"",""}
s
What is that? Where did you see anything like this?
p
when I call the function to preview it, it requires the lambda
first I tryed with FlightsForAirport({})
then, I tryed with, FlightsForAirport({"",""})
as didn't work, I was unable to find why, and openned this question
I still don't understand why you need to put {acc: String, i: String -> } instead of something more logic or simple
like {"",""} or even {}
s
The lambda takes in two parameters. You need to name those two parameters, that's all there is to it. And
_
is a valid name for them I still do not understand why you think
{"",""}
would even remotely make sense here. Your lambda is receiving those two variables, you are not providing any new variables yourself. Also, try to think if those were not Strings at all, you're getting caught up on this for no reason.
p
in my case the lambda requires two strings
I'm calling the function
well, it's hard for me to understand why should I call the function with two names instead of two values
😑
s
You are not calling the function. You are defining what will happen when that function is called. And you are receiving two strings which you can do something with them.
p
what?
check this code, it's from the same preview:
Copy code
val list = listOf(
        Airport(0,"AAA", "AIRPORT A", 10),
        Airport(1,"BBB", "AIRPORT B", 20),
        Airport(2,"CCC", "AIRPORT C", 30),
        Airport(3,"DDD", "AIRPORT D", 40),
        Airport(4,"EEE", "AIRPORT E", 50)
    )

    FlightsForAirport(list[0], list, emptyList())
I see that as a variable and a function which is being called with that variable and some variables more
for me that is to call a function
and the same way I'm passing variables like normally I do in kotlin or java etc... the way I understand it, it's very far from {acc: String, i: String -> }
that doesn't represent to pass a variable, that represent something different
Copy code
FlightsForAirport(list[0], list, emptyList(), {acc: String, i: String -> })
the first three variables are normal, the fourth it's very rare and hard to understand from my point of view, literally I still don't understand what logic haves that
this whould be much more logic and easy to understand:
Copy code
FlightsForAirport(list[0], list, emptyList(), {"",""})
FlightsForAirport(list[0], list, emptyList(), {})
representing that it's an empty lambda, and empty code
I'm starting to think that my way of thinking things has a problem with reactive programming, flows, etc... I see you guys very confortable with this, and I see it as very complex and strange, hard to understand and manage.
I see much more easy the traditional sequential imperative programming, the old style with java, or even kotlin without compose
I remember when I started with java, the learning curve was so fast, and even with android, very fast and easy curve
Now i'm having a lot of problems with compose, flows, corutines, etc... reactive programming in general seems to be much more difficult for me, or at least the way it's implemented in Compose+Flow+Corutines
s
Yeah you seem to be quite confused. You are not passing a value in there. You are passing a lambda which is going to be called with two parameters, and in this case it happens to be two strings. I really suggest you take a step back, learn more about Kotlin itself as a language and then resume learning compose, coroutines etc on top of all this. You ought to be able to handle the language itself well before being able to use such things which admittedly are much more complicated. Have you done Kotlin Koans? https://kotlinlang.org/docs/koans.html This was my first step coming into Kotlin from Java and it helped me a ton! I highly suggest it.
p
thank you, I didn't know koans, but I already did various kotlin alone (without android) courses and have been already two years using it
I'm now doing the android course with compose, which is very long
I'm not an expert for sure in lambdas, they are probably the hardest part of kotlin for me, but the most of the problems I'm finding are related to compose, flow and corutines, specifically related with reactive programming
s
Also for this particular thing, check out the docs here https://kotlinlang.org/docs/lambdas.html
p
thanks
s
Yes, this issue in particular has nothing to do with compose or coroutines, the last linked docs about higher order functions should help you understand why "","" does not work there