I have a model data class with a few attributes. I have added some data into the Firestore
and read them using this model class. Later, I decided to add a few more attributes in the model class but I have not added new fields in the Firestore
database. When I read the data like before, my app crashes. This is obvious that as we go further we would add more details but we may or may not add these new details to old items that are already added to the database. How can I handle this situation?
This is how I am reading from the `Firestore'
mFireStore.collection("product_details")
.get()
.addOnCompleteListener {
if (it.isSuccessful) {
for (document in it.result) {
val product = document.toObject(ProductDetails::class.java)
product.product_id = document.id
productsList.add(product)
}
} else {
}
tempProductsList.addAll(productsList)
listProducts()
}
.addOnFailureListener {
}
and this is where I am getting the error.
val product = document.toObject(ProductDetails::class.java)
Edit:
A sample of my model class is provided below. It has a lot of fields but I copied only a few here. 'min_order_quantity' and 'max_order_quantity' are the two that I newly added in this model class and the items already in the Firestore
doesn't have these fields.
@Parcelize
data class ProductDetails(
var product_id: String? = "",
val title: String = "",
val mrp: String= "0.00",
val price: String = "0.00",
val description: String = "",
var min_order_quantity: String="",
var max_order_quantity: String="",
) : Parcelable
Logcat:
Process: com.abc.xyz, PID: 19223
java.lang.VerifyError: Verifier rejected class com.abc.xyz.models.ProductDetails: void com.abc.xyz.models.ProductDetails.<init>() failed to verify: void com.abc.xyz.models.ProductDetails.<init>(): [0x1FB] Rejecting invocation, expected 1 argument registers, method signature has 2 or more (declaration of 'com.abc.xyz.models.ProductDetails' appears in /data/data/com.abc.xyz/code_cache/.overlay/base.apk/classes5.dex)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.google.firebase.firestore.util.ApiUtil.newInstance(ApiUtil.java:43)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:757)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:741)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:542)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:253)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:100)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:116)
at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(QueryDocumentSnapshot.java:97)
at com.abc.xyz.ui.fragments.HomeFragment.getProductList$lambda-7(HomeFragment.kt:431)
at com.abc.xyz.ui.fragments.HomeFragment.lambda$QeVGu-lWsXTqkddk(Unknown Source:0)
at com.abc.xyz.ui.fragments.-$$Lambda$HomeFragment$QeVGu-lWsXTqkddk.onComplete(Unknown Source:2)
at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)