英文:
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 (). 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)
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)
-
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
)
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论