在将 Firestore 中的数据转换为对象后返回 Null。

huangapple go评论79阅读模式
英文:

Return Null after convert toObjects in Firestore

问题

data class Course (
    val title: String? = null,
    val image: String? = null,
    val guid: String? = null,
    val price: String? = null,
    val category: String? = null,
    val instructorName: String? = null,
    val instructorCode: String? = null,
    val rating: String? = null
)

val progressOne = async {
    firestore.collection("courses_popular")
        .get()
        .addOnCompleteListener { task ->
            if(task.isSuccessful){
                val doc = task.result
                if(doc != null){
                    val res = doc.toObjects(Course::class.java)
                    popularData.postValue(Response(res, ResponseStatus.SUCCESS))
                }else{
                    popularData.postValue(Response(responseStatus = ResponseStatus.ERROR, msg = "Document null"))
                }
            }
            else{
                popularData.postValue(Response(responseStatus = ResponseStatus.ERROR, msg = "${task.exception?.message}"))
            }
        }
}
data class Course (
    val title: String? = null,
    val image: String? = null,
    val guid: String? = null,
    val price: String? = null,
    val category: String? = null,
    val instructorname: String? = null,
    val instructorcode: String? = null,
    val rating: String? = null
)

Error console

D/MateriPopulerAdapter: Course(title=Belajar Matematika tingkat SMP, image=https://storage.googleapis.com/skill-baru.appspot.com/courses_popular/-MI7nXMm1EsCqQX2Shfr/ilustrasi-matematika.jpg, guid=1, price=500000, category=MTK, instructorname=null, instructorcode=null, rating=4)
10-15 15:20:58.984 13736-13736/com.skillbaru.apps W/TabLayout: MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
10-15 15:20:59.005 13736-13736/com.skillbaru.apps D/AndroidRuntime: Shutting down VM

Note: The provided content contained images referenced using markdown notation (在将 Firestore 中的数据转换为对象后返回 Null。). However, as a text-based AI, I can't interpret or display images. If you need assistance with image-related information, please describe the content, and I'll be happy to help.

英文:

I have data class like below

data class Course (
    val title: String?=null,
    val image: String?=null,
    val guid: String?=null,
    val price: String?=null,
    val category: String?=null,
    val instructorName: String?=null,
    val instructorCode: String?=null,
    val rating: String?=null
)

and when i push data everything ok, i have my data in firestore like this(sorry i have 2 kind of data because try and error)
在将 Firestore 中的数据转换为对象后返回 Null。

but my problem is when i use this execution in my viewmodel

val progressOne = async {
                firestore.collection("courses_popular")
                    .get()
                    .addOnCompleteListener {task ->

                        if(task.isSuccessful){
                            val doc = task.result
                            if(doc != null){
                                val res = doc.toObjects(Course::class.java)
                                popularData.postValue(Response(res, ResponseStatus.SUCCESS))
                            }else{
                                popularData.postValue(Response(responseStatus = ResponseStatus.ERROR, msg = "Document null"))
                            }
                        }
                        else{
                            popularData.postValue(Response(responseStatus = ResponseStatus.ERROR, msg = "${task.exception?.message}"))
                        }
                    }
            }

why i always got instructorName and instructorCode null, however the value are exists?
(this what i got when print console)

> MateriPopulerAdapter: Course(title=Belajar Matematika tingkat SMP,
> image=https://storage.googleapis.com/skill-baru.appspot.com/courses_popular/-MI7nXMm1EsCqQX2Shfr/ilustrasi-matematika.jpg,
> guid=1, price=500000, category=MTK, instructorName=null,
> instructorCode=null, rating=4)

  1. ATTEMP
    change all camel case be normal case.

    data class Course (
    val title: String?=null,
    val image: String?=null,
    val guid: String?=null,
    val price: String?=null,
    val category: String?=null,
    val instructorname: String?=null,
    val instructorcode: String?=null,
    val rating: String?=null
    )

在将 Firestore 中的数据转换为对象后返回 Null。

Error console

D/MateriPopulerAdapter: Course(title=Belajar Matematika tingkat SMP, image=https://storage.googleapis.com/skill-baru.appspot.com/courses_popular/-MI7nXMm1EsCqQX2Shfr/ilustrasi-matematika.jpg, guid=1, price=500000, category=MTK, instructorname=null, instructorcode=null, rating=4)
10-15 15:20:58.984 13736-13736/com.skillbaru.apps W/TabLayout: MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
10-15 15:20:59.005 13736-13736/com.skillbaru.apps D/AndroidRuntime: Shutting down VM

答案1

得分: 2

我解决了这个问题,我所做的是将所有参数(在Firestore和POJO中)都改为小写(不使用驼峰命名法)。还删除了所有的驼峰命名法,并尝试卸载应用程序,以确保本地设备中没有缓存参数。所以现在一切都正常工作。

英文:

I solved the issue, what i did is changing all parameters(in firestore and POJO) become lowercase (no camel case). And also remove all camelcase and try to uninstall the apps to ensure there is no caching params in local device. So now everything works fine.

huangapple
  • 本文由 发表于 2020年10月15日 00:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64357526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定