I want to save data inside the phone without accessing internal storage in device. Through this tutorial https://medium.com/@rowaido.game/persistent-data-storage-using-datastore-preferences-in-jetpack-compose-90c481bfed12 , I used the step by step guide and build and it works great. My issue is the tutorial uses ViewModelProvider.Factory, the tutorial approach doesn’t allow me to send parameters to the viewModel class.
When Ever I want to call MyApplication().UserRepository the error says it has to be initialized
This is my normal factory that I used :
class MyViewModelFactory( private val _mode: String, private val _size: List<Int>, private val _data: String, private val _repository: UserRepository ) : ViewModelProvider.NewInstanceFactory() { override fun <T : ViewModel> create(modelClass: Class<T>): T { return SudokuViewModel(mode=_mode, _size = _size, _data = _data, _repository) as T } }
This the part where i call the factory:
val userRepository: UserRepository = MyApplication().userRepository val viewModel: SudokuViewModel = viewModel( factory = MyViewModelFactory(_mode=mode, _size=size,_data= getSudokuData(context), _repository = userRepository))
the error I get when I run the code:
FATAL EXCEPTION: main Process: com.example.sudokuapp, PID: 14438 kotlin.UninitializedPropertyAccessException: lateinit property userRepository has not been initialized at com.example.sudokuapp.MyApplication.getUserRepository(MyApplication.kt:15) at com.example.sudokuapp.SudokuUIKt.SudokuUI(SudokuUI.kt:59) at com.example.sudokuapp.SudokuAppKt$MyApp$1$2.invoke(SudokuApp.kt:47) at com.example.sudokuapp.SudokuAppKt$MyApp$1$2.invoke(SudokuApp.kt:44) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:139) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.navigation.compose.NavHostKt$NavHost$14$1.invoke(NavHost.kt:308) at androidx.navigation.compose.NavHostKt$NavHost$14$1.invoke(NavHost.kt:306) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:248) at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84) at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:65) at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1) at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:52) at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:51) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35) at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)