tried to make a simple screen with ViewModel targe...
# multiplatform
k
tried to make a simple screen with ViewModel targeting Android, IOS, Web, after adding ViewModel to my composable like below,
Copy code
viewModel: ScreenAViewModel = viewModel { ScreenAViewModel() }
I ran the Web app and it's failing at compilation with the following errors
Copy code
e: user/Demo/composeApp/src/commonMain/kotlin/com/example/demo/screenA/ScreenA.kt:21:37 Unresolved reference 'compose'.
e: user/Demo/composeApp/src/commonMain/kotlin/com/example/demo/screenA/ScreenA.kt:32:33 Cannot infer type for this parameter. Specify it explicitly.
e: user/Demo/composeApp/src/commonMain/kotlin/com/example/demo/screenA/ScreenA.kt:32:33 Parameter 'viewModel' is uninitialized here.
e: user/Demo/composeApp/src/commonMain/kotlin/com/example/demo/screenA/ScreenA.kt:32:33 Unresolved reference. None of the following candidates is applicable because of a receiver type mismatch:
fun <T, R> DeepRecursiveFunction<T, R>.invoke(value: T): R
can please someone explain why is it failing?
Hi guys, not able to resolve this, any help please
z
It's really hard to tell from your one line of code, that seems fine on its own. Could you provide the entire file, or perhaps share a project that reproduces the issue on GitHub or in a YouTrack issue?
k
sure here's the file, can't share the repo as it is an org's private repo
Copy code
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import cmp_di_poc.composeapp.generated.resources.Res
import cmp_di_poc.composeapp.generated.resources.compose_multiplatform
import com.egor.example.cmp_di_poc.screenB.ScreenBJourney
import org.jetbrains.compose.resources.painterResource

@Composable
fun ScreenA(
  navController: NavController,
  viewModel: ScreenAViewModel = viewModel { ScreenAViewModel() }
) {
  var showContent by remember { mutableStateOf(false) }
  Column(
    modifier = Modifier
      .safeContentPadding()
      .fillMaxSize(),
    horizontalAlignment = Alignment.CenterHorizontally,
  ) {
    Button(onClick = { navController.navigate(ScreenBJourney) }) {
      Text("Go to ScreenB")
    }
    Button(onClick = { showContent = !showContent }) {
      Text("Click me!")
    }
    AnimatedVisibility(showContent) {
      //val greeting = remember { Greeting().greet() }
      Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
        Image(painterResource(Res.drawable.compose_multiplatform), null)
        //Text("Compose: $greeting")
      }
    }
  }
}
🧵 3
here's the ViewModel
Copy code
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class ScreenAViewModel : ViewModel() {
  private val _imageStateFlow = MutableStateFlow(false)
  val imageStateFlow = _imageStateFlow.asStateFlow()

  fun showContent(isVisible: Boolean) {
    println("isVisible: $isVisible")
    _imageStateFlow.value = isVisible
  }
}
z
My guess is that you're missing the dependency that the
viewModel
function comes from, so
Copy code
org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose
The version would be the same as other lifecycle dependencies you have, to have the
ViewModel
class in the first place. This just adds the Compose integrations like the
viewModel {}
function.
(PS. Please don't post additional messages from the thread to the channel)
k
My bad, raised a very lame ticket, it was written in the document still missed to add this dependency Thanks for the help😄 Understood I won't post thread messages in the channel from now on.