I am facing an issue while integrating Hilt with my view Model. The application gets crash right away after the launch. Any will be appreciated because I may be making a basic mistake.
//Here is my code for MainActivity:
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NoteJetTheme {
// A surface container using the 'background' color from the theme
// val noteViewModel = viewModel<NoteViewModel>()
val noteViewModel: NoteViewModel by viewModels()
NoteApp(noteViewModel = noteViewModel)
}
}
}
}
//and here's the viewModel code:
@HiltViewModel
class NoteViewModel @Inject constructor(private val repo : NoteRepo): ViewModel() {
var \_noteList = MutableStateFlow\<List\<UserNote\>\>(emptyList())
var noteList = \_noteList.asStateFlow()
init {
// noteList.addAll(NoteDataSource().loadNotes())
viewModelScope.launch(Dispatchers.IO) {
repo.getAllNotes().distinctUntilChanged()
.collect{ listOfNote ->
if(listOfNote.isNotEmpty())
_noteList.value = listOfNote
else
Log.d("noteListFlow", "list is empty")
}
}
}
These are the libraries that I used in this project.
// here are the libs
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
implementation 'androidx.compose.material:material:1.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'