如何使用 HiltViewModel 库和 jetpack compose 将参数传递给 viewmodel 构造函数 我的代码:
@HiltViewModel
class GetPurchaseViewModel @Inject constructor(val id:Long) : ViewModel() {
private val service= RestModule
var state = MutableStateFlow<State>(State.START)
init{
get(id)
}
private fun get(id:Long){
viewModelScope.launch {
state.value = State.LOADING
try {
val users = withContext(Dispatchers.IO) {
service.getIntance().getPurchase(id)
}
state.value = State.SUCCESLISTPURCHASE(users)
} catch (e: Exception) {
state.value = State.FAILURE(message = e.localizedMessage!!)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在可组合函数中的调用:
val model:GetPurchaseViewModel=hiltViewModel(idUser)
Run Code Online (Sandbox Code Playgroud) 不知何故,构建崩溃了,并出现了与导航组件相关的奇怪错误,尽管它以前可以工作,但错误出在生成的类中,就我而言 NativeLanguageSelectionFragmentDirections
这是错误
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (10, 16): Class 'GoToSelectLearningLangAction' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (13, 12): 'getActionId' overrides nothing
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (15, 12): 'getArguments' overrides nothing
Run Code Online (Sandbox Code Playgroud)
这是导航图的代码
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (10, 16): Class 'GoToSelectLearningLangAction' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (13, 12): 'getActionId' overrides nothing
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (15, 12): 'getArguments' overrides nothing
Run Code Online (Sandbox Code Playgroud)
我在用
"androidx.navigation:navigation-fragment-ktx:2.3.5" …Run Code Online (Sandbox Code Playgroud)