Error: "Type mismatch. Required: Observer<PagedList<MyItem!>!>". How to fix?

3.3k Views Asked by At

I'm using PagedListAdapter and pagedListLiveData without Room to display a large list of items.
In code below i'm trying to submit adapter to PagedList.

val pagedListLiveData = LivePagedListBuilder(sourceFactory, pagedListConfig)
       .setFetchExecutor(Executors.newSingleThreadExecutor())
       .build()

val adapter = MyPagedAdapter()

// error below: Type mismatch. Required:Observer<PagedList<MyItem!>!>
pagedListLiveData.observe(lifeCycleOwner, object : Observer<PagedList<MyItem>>() {
        override fun onChanged(items: PagedList<MyItem>) {
            Log.d(LOG_TAG, "submit PagedList")
            adapter.submitList(items)
        }
})

Android Studio highlights an error on the expression:

object : Observer<PagedList<MyItem>>

Error: Type mismatch. Required: Observer<PagedList<MyItem!>!>

How to fix it?

3

There are 3 best solutions below

0
Gregory On BEST ANSWER

Thanks to Kingfisher Phuoc and Jeel Vankhede. Problem was in importing Observer. This code works fine:

pagedListLiveData.observe(lifeCycleOwner, android.arch.lifecycle.Observer{
    adapter.submitList(it)
})
1
Kingfisher Phuoc On

it's your observer problem. You can just do something like below:

pagedListLiveData.observe(lifeCycleOwner, Observer{
     adapter.submitList(it)
})

Otherwise, you should make sure PagedList<MyItem> not null by using PagedList<MyItem!>!

0
Kostya  Mistery On

For those who are here because, you have deleted "Observer" word from LiveData subscriptions before. But something went wrong and after a while Android Studio starts to ask you to bring Observer back. With error like "Type mismatch. Required: Observer Found: () -> Unit"

  1. Go to File->Settings->Languages & Frameworks->Kotlin
  2. Switch "Update chanel" to "Stable"
  3. Click "Check again" button.
  4. Click "Install" button.
  5. Restart Android Studio

P.S. If you don't have Install button, you may also need to switch Update chanel from "Stable" to something else, and then make it "Stable" again.