Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SavedStateHandle KMM Compose Issue #2117

Open
ErickSorto opened this issue Jan 17, 2025 · 0 comments
Open

SavedStateHandle KMM Compose Issue #2117

ErickSorto opened this issue Jan 17, 2025 · 0 comments

Comments

@ErickSorto
Copy link

Describe the bug
Value not being registered in ViewModel when injecting SavedStateHandle

Koin module and version:
4.1.0-Beta5

To Reproduce
TypeSafe Navigation:

composable<Route.AddEditDreamScreen> { backStackEntry ->
                val args = backStackEntry.toRoute<Route.AddEditDreamScreen>()
                val image = args.backgroundID
                
                val addEditDreamViewModel = koinViewModel<AddEditDreamViewModel>()
                
                println("Dream ID: ${navController.currentBackStackEntry?.savedStateHandle?.get<String>("dreamID")}")
                AddEditDreamScreen(
                    dreamImage = image,
                    onMainEvent = { onMainEvent(it) },
                    onAddEditDreamEvent = { addEditDreamViewModel.onEvent(it) },
                    animateVisibilityScope = this,
                    onNavigateToDreamJournalScreen = {
                        navController.popBackStack()
                        navController.navigate(Route.DreamJournalScreen)
                    },
                    onImageClick = { imageID ->
                        navController.navigate(
                            Route.FullScreenImageScreen(imageID)
                        )
                    }
                )
            }

Koin:

val viewModelModule = module {
    viewModelOf(::AddEditDreamViewModel)
}

ViewModel:

class AddEditDreamViewModel(
    private val savedStateHandle: SavedStateHandle,
    private val dreamUseCases: DreamUseCases,
    private val authRepository: AuthRepository,
    private val dictionaryRepository: DictionaryRepository,
    private val vibratorUtil: VibratorUtil,
) : ViewModel() {

    private val _addEditDreamState = MutableStateFlow(
        AddEditDreamState(
            authRepository = authRepository
        )
    )
    val addEditDreamState: StateFlow<AddEditDreamState> = _addEditDreamState.asStateFlow()
    

    init {
        savedStateHandle.get<String>("dreamID")?.let { dreamId -> //value is Null (worked before transition to KMM)
        }
    }

This problem started after I transitioned my project from Android to KMM. Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant