【问题标题】:Cannot access class 'com.google.firebase.firestore.DocumentReference'无法访问类“com.google.firebase.firestore.DocumentReference”
【发布时间】:2021-04-21 02:52:38
【问题描述】:

我正在开发一个 Android 项目,并且我使用 Firebase 作为我的后端。我创建了一个模块来处理所有与 Firestore 相关的工作。在这个模块中,我创建了这个类:

import com.google.firebase.firestore.DocumentReference
import java.util.*

data class SurveyDto(
        var id: String = "",
        var name: String = "",
        var type: String = "",
        var creationDate: Date = Date(),
        var createdBy: DocumentReference? = null,
        var current: Boolean = true,
        var sections: List<SectionDto> = listOf()
)

在我的主模块中,我创建了一个 Mapper 类,它将负责将我的 SurveyDto 对象转换为 Survey 对象(为简单起见省略了映射逻辑):

class SectionDtoMapper: DomainMapper<SectionDto, Section> {

    private val questionDtoMapper = QuestionDtoMapper()

    override fun mapToDomainModel(model: SectionDto): Section {
        return Section()
    }

    override fun mapFromDomainModel(domainModel: Section): SectionDto {
        return SectionDto()
    }

}

问题是,如果我尝试运行该应用程序,它会在 return SectionDto() 处显示此运行时异常:

无法访问“com.google.firebase.firestore.DocumentReference”类。检查您的模块类路径是否存在缺失或冲突的依赖项

如果我从SurveyDto 类中删除var createdBy: DocumentReference? = null 属性,或者如果我删除return SectionDto() 代码,应用程序运行正常。

我正在将它导入我的 firebase 模块:

implementation platform("com.google.firebase:firebase-bom:26.8.0")
implementation "com.google.firebase:firebase-firestore-ktx"

// Some other firebase and non firebase imports

在我的主模块上,我正在导入:

implementation platform(rootProject.ext.firebase.bom)
implementation project(':libraries:firebase_api')

// Some other firebase and non firebase imports

请记住,我有很多导入 DocumentReference 的对象,但它们都是用 Java 编写的……不知道这是否相关!

你知道哪里出了问题吗?

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    所以,我解决了我的问题,为我的数据类手动添加一个空构造函数:

    import com.google.firebase.firestore.DocumentReference
    import java.util.*
    
    data class SurveyDto(
            var id: String,
            var name: String,
            var type: String,
            var creationDate: Date,
            var createdBy: DocumentReference?,
            var current: Boolean,
            var sections: List<SectionDto>
    ) {
        constructor() : this(
            id = "",
            name = "",
            type = "",
            creationDate = Date(),
            createdBy = null,
            current = true,
            sections = listOf()
        )
    }
    

    我不知道为什么这解决了我的问题...我一直认为为每个字段分配默认值与创建辅助构造函数相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      相关资源
      最近更新 更多