Thread
#compose
    Colton Idle

    Colton Idle

    1 year ago
    Am I doing anything wrong here? Can't get
    viewModel()
    to compile. I've added the lifecycle compose vm dependency
    i

    Ian Lake

    1 year ago
    Sounds like you have the wrong import
    steelahhh

    steelahhh

    1 year ago
    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

    1 year ago
    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
    Colton Idle

    Colton Idle

    1 year ago
    My imports
    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
    // 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

    1 year ago
    I don't see any
    androidx.lifecycle.viewmodel.compose.viewModel
    import
    Colton Idle

    Colton Idle

    1 year ago
    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

    1 year ago
    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)
    Colton Idle

    Colton Idle

    1 year ago
    I am a bug filing machine!
    Officially hit 100 bugs submitted as of ~last week
    k

    Kefas

    1 year ago
    Have you fixed this? I think you can't set the param name with
    viewModel
    i

    Ian Lake

    1 year ago
    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
    Colton Idle

    Colton Idle

    1 year ago
    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

    1 year ago
    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.
    Colton Idle

    Colton Idle

    1 year ago
    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

    1 year ago
    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
    Colton Idle

    Colton Idle

    1 year ago
    ah. gotcha. thanks for clarifying