https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

07/23/2021, 2:02 PM
Am I doing anything wrong here? Can't get
viewModel()
to compile. I've added the lifecycle compose vm dependency
i

Ian Lake

07/23/2021, 2:03 PM
Sounds like you have the wrong import
s

steelahhh

07/23/2021, 2:05 PM
Looks like it’s not using the import and trying to call the parameter itself - try a different name for it perhaps
i

Ian Lake

07/23/2021, 2:06 PM
When in doubt, control+click on it and see what it is trying to use. If that doesn't jump you to the code you expect, then you'll be able to figure out what is conflicting
c

Colton Idle

07/23/2021, 2:07 PM
My imports
Copy code
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModel
import androidx.navigation.NavController
Can't cmd + click as I get "Cannot find declaration to go to"
My deps
Copy code
// Compose helpers
    implementation("androidx.activity:activity-compose:1.3.0-rc02")
    implementation("androidx.navigation:navigation-compose:2.4.0-alpha04")
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07")
    implementation("androidx.hilt:hilt-navigation-compose:1.0.0-alpha03")
I assume I'm missing something basic here. Maybe another dep? But I've been going through the docs and I seem pretty solid.
i

Ian Lake

07/23/2021, 2:08 PM
I don't see any
androidx.lifecycle.viewmodel.compose.viewModel
import
c

Colton Idle

07/23/2021, 2:10 PM
Jeez.
Why does android studio not give the right import hints anymore. 😭
I've seen this a lot with working with compose code.
Adding it myself worked. Thanks @Ian Lake ¯\_(ツ)_/¯
i

Ian Lake

07/23/2021, 2:17 PM
Now I'm curious to know what any of those quick fixes actually do
(and you should file a bug against Studio if it reproduces for you in a sample project)
c

Colton Idle

07/23/2021, 2:19 PM
I am a bug filing machine!
Officially hit 100 bugs submitted as of ~last week
🎉 17
k

Kefas

07/24/2021, 7:07 PM
Have you fixed this? I think you can't set the param name with
viewModel
i

Ian Lake

07/24/2021, 11:43 PM
Using
viewModel()
for a parameter is perfectly fine (although relying on actual state, etc. from your ViewModel will make it more testable if you don't want to mock your ViewModel / have your own set of fakes). All Colton was missing was the import
c

Colton Idle

07/24/2021, 11:45 PM
Correct. I was just missing an import. Afterwards, I went onto hilt and AAC compose navigation and I had to use hiltViewModel() or else I got a crash at runtime.
k

Kefas

07/25/2021, 1:47 AM
I mean the function parameter's name. If I use
viewModel
, the auto-import doesn't work. But if I use something else for the name (e.g.
homeViewModel
), the auto-import works.
c

Colton Idle

07/25/2021, 1:52 AM
I just had to import manually. It's still on my list to create a repro project for this and file a bug. If you have a simple repro for this, please file to help the next person out!
i

Ian Lake

07/25/2021, 3:54 AM
Ah, I think what Kefas means is that Studio thinks that you're trying to use the variable called
viewModel
and call its
invoke()
method, which is shorthanded to
()
, which is why it was recommending creating that method. Calling the variable anything else removes the ambiguity and lets Studio recognize it as an unknown symbol which it can then offer to solve via an import
☝️ 1
c

Colton Idle

07/25/2021, 3:55 AM
ah. gotcha. thanks for clarifying