Am I doing anything wrong here? Can't get `viewMod...
# compose
c
Am I doing anything wrong here? Can't get
viewModel()
to compile. I've added the lifecycle compose vm dependency
i
Sounds like you have the wrong import
s
Looks like it’s not using the import and trying to call the parameter itself - try a different name for it perhaps
i
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
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
I don't see any
androidx.lifecycle.viewmodel.compose.viewModel
import
c
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
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
I am a bug filing machine!
Officially hit 100 bugs submitted as of ~last week
🎉 17
k
Have you fixed this? I think you can't set the param name with
viewModel
i
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
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
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
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
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
ah. gotcha. thanks for clarifying