title: Cannot create an instance of viewmodel while using Jetpack Compose navigation
body: <p>I am using navigation component for jetpack compose in my app like this:</p>
  <pre><code>@Composable
  fun FoodiumNavigation() {
      val navController = rememberNavController()
  
      NavHost(
          navController = navController,
          startDestination = Screen.Main.route,
      ) {
          composable(Screen.Main.route) {
              MainScreen(navController)
          }
  
          ...
      }
  }
  </code></pre>
  <p>And I am getting <code>viewmodel</code> in my <code>MainScreen</code> composable like this:</p>
  <pre><code>@Composable
  fun MainScreen(navController: NavController) {
      val mainViewModel: MainViewModel = viewModel()
      ...
  }
  </code></pre>
  <p>which is giving me a runtime exception as <code>Cannot create an instance of class com.package.main.MainViewModel</code>.</p>
  <p>Here, I am stating that this only happens while using navigation component, i.e. everything was working fine and <code>mainViewModel</code> was successfully instantiated before using navigation component in my app.</p>
  <p>The <code>MainViewModel</code> is like this:</p>
  <pre><code>@ExperimentalCoroutinesApi
  @HiltViewModel
  class MainViewModel @Inject constructor(private val postRepository: PostRepository) :
      ViewModel() {
  
      private val _postsLiveDataState = MutableLiveData&lt;UiState&lt;List&lt;Post&gt;&gt;&gt;()
      val postLiveState: LiveData&lt;UiState&lt;List&lt;Post&gt;&gt;&gt; = _postsLiveDataState
  
      init {
          getPostsState()
      }
  
      private fun getPostsState() {
          viewModelScope.launch {
              postRepository.getAllPosts()
                  .onStart { _postsLiveDataState.value = UiState(loading = true) }
                  .map { resource -&gt; UiState.fromResource(resource) }
                  .collect { state -&gt; _postsLiveDataState.value = state }
          }
      }
  }
  </code></pre>