默认 FirebaseApp 未在此过程中初始化...确保首先调用 FirebaseApp.initializeApp(Context)

我正在尝试将数据添加到 Firestore。我也想用匕首做到这一点。但我不断收到此错误。你能帮我吗......

默认 FirebaseApp 未在此进程 com.example.vvvv 中初始化。确保首先调用 FirebaseApp.initializeApp(Context)。

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")
        }
    }
  }
 }

视图模型

@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)
    }
   }
}

应用模块

@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")
}
stack overflow Default FirebaseApp is not initialized in this process ... Make sure to call FirebaseApp.initializeApp(Context) first
原文答案
author avatar

接受的答案

我在这个项目中使用了多模块。 id com.google.gms.google-services 到应用程序目录并解决。


答案:

作者头像
  1. 在依赖项的 build.gradle(project) 中添加以下内容:

      classpath 'com.google.gms:google-services:4.3.5'
    
  2. 在 build.gradle(module) 中添加以下内容:

    plugins {
    id 'com.google.gms.google-services'
     }
    
  3. 在您的最后一行代码中添加以下内容:

    apply plugin: 'com.google.gms.google-services'