I have a below sample code where i am collecting uiState in fragment. Due to some reason uiState.collect not firing. Anything missing below code?
@AndroidEntryPoint
class NewsFragment : Fragment() {
private lateinit var binding: FragmentNewsBinding
private val viewModel by viewModels<NewsViewModel>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentNewsBinding.inflate(inflater, container, false)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { uiState ->
Log.d("TAG", "collect")
}
}
}
return binding.root
}
ViewModel
@HiltViewModel
class NewsViewModel @Inject constructor(
private val repository: Repository,
) : ViewModel() {
private var _uiState = MutableStateFlow(UiState(isLoading = false))
val uiState = _uiState.asStateFlow()
}
data class UiState(
val isLoading = false,
val list = emptyList()
)