Kodein Framework - property delegate must have a provideDelegate(...) method

2.3k Views Asked by At

I'm trying to build an app with the following architecture: LoginActivity -> MainActivity -> everything else handled in fragments hosted by MainActivity. I'm also using the Kodein Framework for the first time and get the following error in my starting fragment:

Property delegate must have a 'provideDelegate(HomeFragment, KProperty*>' method. None of the following functions is suitable. provideDelegate(Context [highlighted in red], KProperty<>?) defined in org.kodein.di.android.KodeinPropertyDelegateProvider Type 'KodeinPropertyDelegateProvider' has no method 'getValue(HomeFragment, KProperty<>)' and thus it cannot serve as a delegate

This is my code so far:

class HomeFragment : Fragment(), KodeinAware {

override val kodein by kodein()
private val factory : MainViewModelFactory by instance()
private lateinit var viewModel: MainViewModel

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val binding : FragmentHomeBinding = FragmentHomeBinding.inflate(inflater, container, false)

    viewModel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)
    binding.viewModel = viewModel

    return binding.root
}

}

How can I fix this?

Thanks :)

3

There are 3 best solutions below

2
On BEST ANSWER

Nevermind, adding a type declaration after kodein did the trick... :)

0
On

You can do it like this:

override val kodein:Kodein by kodein()
0
On

In your imports change

import org.kodein.di.android.kodein

to

import org.kodein.di.android.x.kodein