I can't give fragment as argument inside ViewModelProviders.of() function

48 Views Asked by At

I want to define ViewModel in fragment but the function is not working properly.

I created a ViewModel class as FeedViewModel . And now I want to define it in fragment but the function is not working properly.I checked out the gradle file but can't find the solution. of function is wrong

Error Message :None of the following functions can be called with the arguments supplied. of(Fragment) defined in android.arch.lifecycle.ViewModelProviders of(FragmentActivity) defined in android.arch.lifecycle.ViewModelProviders


private lateinit var viewModel : FeedViewModel

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    viewModel=ViewModelProviders.of(this).get(FeedViewModel::class.java)
  //  viewModel.refreshData()


}

Image

1

There are 1 best solutions below

0
On BEST ANSWER

Try:

viewModel = ViewModelProvider(this)[FeedViewModel::class.java]