admin管理员组文章数量:1122846
I'm using Hilt 2.52 and trying to use an AssitedInject Viewmodel in a composable function. But each time the application starts and try to Navigate to the User Profie screen, the android app crashes with the following error
java.lang.IllegalStateException: Found @HiltViewModel-annotated class UserProfileViewModel using @AssistedInject but no creation callback was provided in CreationExtras.
Here is my ViewModel definition
@HiltViewModel(assistedFactory = UserProfileViewModel.Factory::class)
class UserProfileViewModel @AssistedInject constructor(
private val sharedPreferences: EngClubSharedPreferences,
private val authRepository: AuthRepository,
private val resourceRepo: FileResourceRepository,
@ApplicationContext private val context: Context,
@Assisted private val user: User?
): ViewModel() {
@AssistedFactory
interface Factory {
fun create(user: User?): UserProfileViewModel
}
}
And I'm trying to use it this way in the composable function
@Composable
fun UserProfileScreen(
modifier: Modifier = Modifier,
isSessionUser: Boolean = true,
user: User? = sessionUser,
onNavigateUp: () -> Unit,
onLogoutOrDeletedAccount: () -> Unit
) {
val navController = rememberNavController()
val viewModel: UserProfileViewModel = hiltViewModel(
creationCallback = { factory: UserProfileViewModel.Factory -> factory.create(user) }
)
NavHost(
modifier = modifier.fillMaxSize(),
navController = navController,
startDestination = ProfileViewingScreen.route
) {
composable(route = ProfileViewingScreen.route) {
UserProfileViewingScreen(
isSessionUser = isSessionUser,
viewModel = viewModel,
onNavigateUp = onNavigateUp,
navigateToEditScreen = { navController.navigate(EditProfileScreen.route) },
onLogoutOrDeletedAccount = onLogoutOrDeletedAccount
)
}
}
}
My question is, what could be the cause of that error? I have a similar setup somewhere else in my codebase and that particular setup works very fine without issues. Am I missing something? I'm doing this somewhere else and it works just fine, but the one above ain't
composable(
route = "${MessageClubRoute.path}?groupID={groupID}",
arguments = listOf(navArgument("groupID") {
type = NavType.StringType
defaultValue = ""
}
)
) {
val groupID = it.arguments?.getString("groupID")
val messagingClubViewModel = hiltViewModel<MessageClubViewModel,MessageClubViewModel.Factory>(
creationCallback = { factory -> factory.create(groupID = "$groupID") }
)
MessageClubRoomNavigation(viewModel = messagingClubViewModel)
}
本文标签:
版权声明:本文标题:android - Compose HiltViewModel: Found HiltViewModel class UserProfileViewMode but no creation callback was provided in Creation 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307477a1933326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论