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)
}

本文标签: