0

My viemodel:

Class CycleVM{

   private val _cycle = MutableStateFlow(Cycle())
   override val cycle: StateFlow<Cycle> = _cycle.asStateFlow()

   override fun updateImg(imgStr: String) {
   
            val c = cycle.value.copy(cycle.value)
           
            c.image = imgStr       

            _cycle.value = c  
        
         
    }

    override fun updateStartDate(date: Date) {
        val c = cycle.value.copy(cycle.value)

        c.startDate = date
        
        _cycle.value = c

    }

} model:

    Class Cycle(
    var image: String? = null
   )  : MyEntity() 
{
    
    fun copy(cycle: Cycle): Cycle {....}
    }

In my composable I call upfdateDate and date changes everywere, but if I call updateImg() allways the _cycle.value.image == null after line "_cycle.value = c". Call this fun from rememberLauncherForActivityResult.

I can change any variable but not cursed it (image). Why? ;( And if I add another action ( c.id = 20) to the update function, then the image assignment occurs. What hapends? :

 override fun updateImg(imgStr: String) {
       
                val c = cycle.value.copy(cycle.value)
               
                c.image = imgStr       
                c.id = 20 //  another action!!!!!!
                _cycle.value = c  
            
             
        }

Add logcat

enter image description here

  • I'm not sure what's going on here tbh, the combination of debugger info and logs seems impossible - are you sure they are from the same run?? Your code is not great, so I would probably start there. Firstly, what does the copy function do? Why you need two instances of Cycle to call it? Make it either static with cycle param, or a member function without param. Why you don't use kotlin data class which will create copy function for you? – Jan Bína Aug 15 '23 at 12:51

1 Answers1

0

made a variable StateFlow in viewmodel for each field of my object recived from database.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 30 '23 at 18:08