For a parameter known only when the ViewModel is created, use assisted injection rather than field injection: ```kotlin @HiltViewModel(assistedFactory = ProductViewModel.Factory::class) class ProductViewModel @AssistedInject constructor( private val repository: ProductRepository, @Assisted private val productId: String ) : ViewModel() { @AssistedFactory interface Factory { fun create(productId: String): ProductViewModel } } ``` Pass the runtime value through the generated assisted factory at ...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill android-di --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Android Di?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-android-di-c8be80d6)More formats (shields.io, HTML) on the badges page.
# Runtime ViewModel parameters
For a parameter known only when the ViewModel is created, use assisted injection rather than field injection:
```kotlin
@HiltViewModel(assistedFactory = ProductViewModel.Factory::class)
class ProductViewModel @AssistedInject constructor(
private val repository: ProductRepository,
@Assisted private val productId: String
) : ViewModel() {
@AssistedFactory
interface Factory {
fun create(productId: String): ProductViewModel
}
}
```
Pass the runtime value through the generated assisted factory at the call site (for example, with the `creationCallback` of Compose `hiltViewModel`):
```kotlin
val viewModel: ProductViewModel = hiltViewModel(
creationCallback = { factory: ProductViewModel.Factory ->
factory.create(productId)
}
)
```
Hilt supplies `ProductRepository`; the caller supplies `productId`. If the ID is a Navigation argument, a `@HiltViewModel` with an injected `SavedStateHandle` is often preferable because the argument is restored automatically:
```kotlin
@HiltViewModel
class ProductViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val repository: ProductRepository
) : ViewModel() {
private val productId: String = checkNotNull(savedStateHandle["productId"])
}
```
Is this your skill, or is something wrong with this listing? . Author removals are honored within 72 hours.
No comments yet. Be the first to comment!