I'm getting another arcane build error. Is Android...
# getting-started
c
I'm getting another arcane build error. Is Android Studio actually stable? It feels incredibly fragile to me.
The error is
Type 'androidx.compose.runtime.State<puzzles.PuzzlesState>' has no method 'getValue(Nothing?, KProperty0<*>)', so it cannot serve as a delegate.
h
Compiler errors are unrelated to the IDE. Did you add the getValue import?
c
What
getValue
import?
h
import androidx.compose.runtime.getValue import androidx.compose.runtime.setValue
c
Why do I need to import a function which isn't referenced in my code?
h
getValue/setValue is the function behind the delegate called by
by
If you only use
val
, you only need the getValue, otherwise you need the write method (setValue) too
And for some reasons these functions are not member functions but extensions requiring an import
c
I wish this were explained somewhere.
I thought I had the ViewModels working, but I restructured some of my code and everything broke. Now components aren't updating and they're reverting to old versions of the state.
None of it seems well-documented and it's incredibly hard to figure out what's talking about Android-only code vs. multiplatform.
And most of the instructions I found conflict with one another.
h
Yeah, the documentation could be improved. The best documentation is the Android one, so I suggest, read the Android documentation first, write some Android apps and then convert it to desktop.
c
Thanks for helping out. It's just really frustrating to follow what's supposed to be a step-by-step tutorial and run into confusing and arcane errors.
In https://developer.android.com/codelabs/basic-android-kotlin-compose-viewmodel-and-state#5, it has the following code:
Copy code
import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun GameScreen(
   gameViewModel: GameViewModel = viewModel()
) {
   // ...
}
But there is no
viewModel
function. The import fails.
How is this broken, and can I get the ViewModel instance from anywhere or is it important to create it in the argument list?
r
I believe that function comes from this artifact
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")
which needs to be included in step 5 https://developer.android.com/codelabs/basic-android-kotlin-compose-viewmodel-and-state#4
c
I have
androidx-lifecycle = { group = "androidx.lifecycle", name="lifecycle-viewmodel-compose", version.ref = "androidx-lifecycle" }
defined in my build, at version 2.8.4