koin
koin copied to clipboard
No value found for type 'androidx.lifecycle.SavedStateHandle'
The bug
App crashes when ViewModel contains SaveStateHandle" in constructor and "by koinNavGraphViewModel" used to initialize in a fragment.
val viewModelModule = module {
viewModel { HomeViewModel(get(), get()) }
viewModel { LoginViewModel(get(), get()) }
viewModel { OnBoardingViewModel(get(), get()) }
viewModel { FlightViewModel(get(), get()) }
viewModel { PaxViewModel(get()) }
}
private val viewModel: PaxViewModel by koinNavGraphViewModel(R.id.pax_graph)
Crash Log
Caused by: org.koin.core.error.DefinitionParameterException: No value found for type 'androidx.lifecycle.SavedStateHandle'
at android.xyz.abc.utils.dependency_injection.ViewModelModuleKt$viewModelModule$1$5.invoke(ViewModelModule.kt:14)
at android.xyz.abc.utils.dependency_injection.ViewModelModuleKt$viewModelModule$1$5.invoke(ViewModelModule.kt:12)
at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:54)
at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:38)
at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:111)
at org.koin.core.scope.Scope.resolveValue(Scope.kt:255)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:242)
at org.koin.core.scope.Scope.get(Scope.kt:205)
at org.koin.androidx.viewmodel.factory.DefaultViewModelFactory.create(DefaultViewModelFactory.kt:13)
at androidx.lifecycle.ViewModelProvider$Factory.create(ViewModelProvider.kt:83)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:53)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:35)
Expected behavior Not supposed to crash or is it?
Koin project used and used version (please complete the following information):
- koin-androidx-navigation version: 3.2.0
- koin-android version: 3.2.0
- lifecycle-viewmodel-savedstate version: 2.5.1
I have the same Issue 😢
Is there any workaround ?
I'm using:
- koin-androidx-compose version: 3.2.0
- koin-android version: 3.2.1
- navigation-compose: 2.5.2
It seems that koinNavGraphViewModel()
doesn't have a SavedStateHandle-supported version yet.
Alternatives such as viewModel()
have stateViewModel()
, and sharedViewModel()
has sharedStateViewModel()
Comparing the differences between them, it seems it simply is the following parameter:
noinline state: BundleDefinition = emptyState(),
I'm not really sure why each method to retrieve a viewModel
needs a separate alternative to retrieve the SavedStateHandle, as it would be great if we didn't need to care about that, but I digress:
Based on the current koinNavGraphViewModel()
, I created the following snippet that seems to work as expected:
@OptIn(KoinInternalApi::class)
inline fun <reified VM : ViewModel> Fragment.koinNavGraphStateViewModel(
@IdRes navGraphId: Int,
noinline parameters: ParametersDefinition? = null,
noinline state: BundleDefinition = emptyState()
): Lazy<VM> {
val backStackEntry: NavBackStackEntry by lazy { findNavController().getBackStackEntry(navGraphId) }
val scope = getKoinScope()
return viewModels(ownerProducer = { backStackEntry }) {
getViewModelFactory<VM>(
owner = backStackEntry,
qualifier = null,
parameters = parameters,
scope = scope,
state = state
)
}
}
Again, I'm not sure if this alternative was simply forgotten or if there is a reason that can cause some problem. If this is fine I can submit a PR adding this function.
Hello,
please update koin-android
in 3.3.0. This update brings fixes for that. Here is the announce: https://blog.kotzilla.io/koin-android-stronger-viewmodel-api/
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.