• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Jetpack Compose viewModel not getting value on method

Resolved: Jetpack Compose viewModel not getting value on method

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a class that has composable functions which is private as well as methods i cant get a value from my view model when accessing it from a normal class method but inside a composable i can access it what can i do?
here is what i tried
What am i doing Wrong here?

Updated


Here is my viewmodel

Answer:

I used the same code as in your ViewModel, but I fixed the compiler error for LiveData<Int> to LiveData<String>. I also changed the empty starting state string to "initial value" for this example.
In your Example class code I fixed another compiler warning where the name of the function you call does not match the actual function name. So I changed state.getSomething() to state.getSomeValue() and I added a Log.d call to log the result.
If you also want to return the result (the value) then add a return statement and change the return type to String for fun getSomeValue().
Then I made a Composable function/component where I call both functions from your class.
And I get the expected results in the log:

If you have better answer, please add a comment about this, thank you!

android android-jetpack-compose android-viewmodel kotlin
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Getting ‘502 Bad Gateway’ while deploying Springboot app in EKS

24/03/2023

Resolved: Why is NGINX’s $request_uri empty?

24/03/2023

Resolved: How to convert Java bytecode to Webassembly using CheerpJ compiler

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.