Reputation: 193
I am trying to add data to firestore. and also i want to do this using dagger. but i keep getting this error:
Default FirebaseApp is not initialized in this process com.example.vvvv.
Make sure to call FirebaseApp.initializeApp(Context) first.
FirestoreREpository
class FirestoreRepository @Inject constructor(private val usersRef:
CollectionReference) {
fun saveSearchResults(userEntities: List<UserEntity>) {
usersRef.add(userEntities).addOnCompleteListener { task ->
when (task.isSuccessful) {
true -> Log.d(ContentValues.TAG, "added:success")
false -> Log.d(ContentValues.TAG, "added:unsuccess")
}
}
}
}
ViewModel
@HiltViewModel
class UserListViewModel @Inject constructor(private val repository: RoomRepository, private
val firestoreRepository: FirestoreRepository) :
ViewModel() {
private val _users = MutableLiveData<List<User>>()
val users: LiveData<List<User>>
get() = _users
var userData: MutableLiveData<List<User>> = MutableLiveData()
fun userSearch(term: String) {
viewModelScope.launch {
loadFromCache(term)
val getPropertiesDeferred =
GithubApi.retrofitService.searchUser(term)
try {
val userEntities: MutableList<UserEntity> = mutableListOf()
val result = getPropertiesDeferred.body()
_users.value = result?.users
result?.users?.forEach {
userEntities.add(
UserEntity(
term = term,
login = it.login,
avatar_url = it.avatar_url
)
)
}
clearSearchResults(term)
updateSearchResults(userEntities, term)
firestoreRepository.saveSearchResults(userEntities) //save
data with firebase
} catch (e: Exception) {
Log.e("userListErr", e.message.toString())
}
}
}
private fun updateSearchResults(userEntities: List<UserEntity>, term:
String) {
viewModelScope.launch(Dispatchers.IO) {
repository.insertSearchResults(userEntities)
loadFromCache(term)
}
}
private fun loadFromCache(term: String) {
viewModelScope.launch(Dispatchers.IO) {
val list = repository.getSearchResults(term)
userData.postValue(list)
}
}
private fun clearSearchResults(term: String) {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteSearchResults(term)
}
}
}
AppModule
@Module
@InstallIn(SingletonComponent::class)
object APPModule {
@Singleton
@Provides
fun getAppDB(context: Application): AppDatabase {
return AppDatabase.getAppDB(context)
}
@Singleton
@Provides
fun getDao(appDB: AppDatabase): AppDao {
return appDB.getDAO()
}
@Provides
fun provideFirebaseFirestore() = FirebaseFirestore.getInstance()
@Provides
fun provideUsersRef(db: FirebaseFirestore) = db.collection("users")
}
Upvotes: 11
Views: 24261
Reputation: 1
I also solved it by downgrading to version 4.3.15
classpath 'com.google.gms:google-services:4.3.15'
Upvotes: 0
Reputation: 121
It seems that classpath 'com.google.gms:google-services:4.4.1'
has an issue. I downgraded version 4.4.1 to 4.3.10 and it's working without error.
You can downgrade or upgrade to another version and try, I think it will work.
Thanks
Upvotes: 1
Reputation: 41
you can just use version 4.3.15 . I have this problem and I solved it by changing the version to "com.google.gms:google-services:4.3.15"
Upvotes: 4
Reputation: 11
Worked for me After downgrade google version classpath 'com.google.gms:google-services:4.3.15'
Upvotes: 1
Reputation: 850
The error occurred when I upgraded to the latest version. There is definitely a better solution, but for now I fixed the error this way. I downgraded "com.google.gms:google-services".
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
classpath 'com.google.gms:google-services:4.4.0'
}
To:
dependencies {
classpath 'com.android.tools.build:gradle:7.3.1'
classpath 'com.google.gms:google-services:4.3.15'
}
Upvotes: 9
Reputation: 577
I meet this problem, because google-service sdk version not work with gradle version.
Maybe you can try with this direction.
The original project build.gradle config is ok.
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:3.2.1"
classpath 'com.google.gms:google-services:4.1.0'
Finally, it's solve by find compatible SDK version.
buildscript {
dependencies {
classpath "com.android.tools.build:gradle:3.3.3"
classpath 'com.google.gms:google-services:4.2.0'
Upvotes: 0
Reputation: 51
In build.gradle(project) in dependencies add the following:
classpath 'com.google.gms:google-services:4.3.5'
In build.gradle(module) add the following:
plugins {
id 'com.google.gms.google-services'
}
in your last line of code add the following:
apply plugin: 'com.google.gms.google-services'
Upvotes: 5
Reputation: 193
I used multi-module in this project. id com.google.gms.google-services
to app directory and solved.
Upvotes: -2